3. Functions
Table of Contents:
Summary
Functions are named containers for statements that can be invoked to execute code, improving readability and reducing repetition.
Parameters are variables defined in a function declaration, allowing functions to accept input and change behavior. Arguments are the actual values passed when calling a function.
Return statements allow functions to produce values that can be used elsewhere in your program and terminate function execution.
Scope determines where variables can be accessed. Global scope variables are accessible everywhere, while local scope variables are only accessible within their function or block.
There are multiple ways to define functions: arrow functions (preferred), function declarations, and function expressions. Arrow functions can use implicit returns for concise syntax.
Using functions helps you follow the "Don't Repeat Yourself" (DRY) principle and write more maintainable code.
Variables Review
Recall that variables are named containers for data.
Suppose we wanted to sum, average, and then print the same message as above but with a different set of numbers. We could write code like this:
Functions
A Function is a reusable container of statements:
Functions improve the readability of our code and minimize repetition of commonly used statements.
A function can be invoked to execute its statements. A function can be invoked many times.
The same result can be achieved with a printSumAndAverage function:
We first create the function to contain the logic, give it a descriptive name, and then invoke the function to execute its statements with minimal repetition.
Challenge: Refactor this highly repetitive code for converting a temperature from fahrenheit into celsius. What should the function be called? What are the inputs that make each instance of code different (and what should those parameters be called)?
Function Calls
After a function is declared, the function must be invoked in order to use the function's code.
Invoking (or "calling") a function causes the program to jump to the first line of code in the function, setting it as the next line of code to be executed.
Note that the order in which statements are executed in our code is not always top to bottom. Declaring the function doesn't cause the code inside to run. We only execute the code inside of sayHello when it is invoked a few lines later.
Experiment: Run this program using the debugger to see how the control flow of the program is affected by invoking a function.
Parameters and Arguments
The function sayHello does the same thing. Every. Single. Time. It always prints "hello".
Parameters are variables created inside of a function that enable a function to change its behavior based on arguments provided in the function call.
Parameters go in the parentheses of a function declaration (separated by commas).
Arguments go in the parentheses of the function call (also separated by commas).
The order of the arguments must match the order of the parentheses.
Challenge: Refactor this function so that it can print any name and hobby.
Test your code by invoking the function with various inputs. What happens when no input is provided?
Return Statements
The functions above use console.log to print out a result to the console, but that result can't be used later in the program. If we want a function to produce a value that we can be used outside of the function, we add a return statement.
A return statement does two things:
It returns a value to the location of the function call.
It terminates the execution of the function
Function Call Resolution
When a function like console.log() has another function call inside, like add(5,3), the inner function call is resolved first.
Challenge: What is the result of this expression? Run this with the debugger to find out!
Multiple Returns
A function can have multiple return statements, but only one is ever executed. This is helpful if the value returned depends on the provided argument.
Arrow Functions vs. Function Declarations vs. Function Expressions
There are many ways to create a function, but we will use Arrow Function syntax:
One nice feature of arrow functions is the implicit return syntax: if the function's body is just a return statement, you can omit the {} and the return keyword.
Challenge: Which of these functions can use implicit returns? Refactor them!
Last updated