JavaScript Functions
Functions are Code Blocks
Functions arereusable code blocks designed to perform a particular task.
Functionsare executed when they arecalled orinvoked.
Functions arefundamental in all programming languages.
Why Use Functions?
Functions help you to:
- Reuse code (write once, run many times)
- Organize code into smaller parts
- Make code easier to read and maintain
What Does a Function Look Like?
A function can be created with thefunction keyword, aname, andparentheses.
The code to run is written insidecurly brackets.
Example
A one liner:
return "Hello World";
}
Note
The function above does not do anything.
It has to be called first.
Functions Run When You Call Them
To run a function, you call it by using its name followed by parentheses likesayHello():
Note
() means execute now.
JavaScript Function Syntax
// code to be executed
}
Functions are defined with thefunction keyword:
- followed by the functionname
- followed byparentheses ( )
- followed bybrackets { }
Thefunction name follows the naming rules for variables.
Optional parameters are listed inside parentheses:( p1, p2, ...)
Code to be executed is listed inside curly brackets:{ }
Functions canreturn an optional value back to the caller.
Note
A function definition is not an executable statement.
It is not common to end afunction definition with a semicolon.
Semicolons are used to separate executable JavaScriptstatements.
A Function Can Be Used Many Times
A big benefit is that you can call the same function whenever you need it.
Example
return a + b;
}
let sum1 = add(5, 5);
let sum2 = add(50, 50);
Note
Note that values returned from functions can be stored in variables.
Local Variables
Variables declaredwithin a JavaScript function, becomeLOCAL to the function.
Local variables can only be accessed from within the function.
Example
function myFunction() {
let carName = "Volvo";
// code here CAN use carName
}
// code here can NOT use carName
Since local variables are only recognized inside their functions, variables with the same name can be used in different functions.
Note
Local variables are created when a function starts, and deleted when the function is completed.
Functions Used as Variables
Functions can be used as variables, in all types of formulas, assignments, and calculations.
Example
Instead of using a variable to store the return value of a function:
let text = "The temperature is " + x + " Celsius";
You can use the function directly, as a variable value:
Why Functions?
Functions enablebetter code organization and efficiency.
With functions you canreuse the same code many times.
The same code, with different input, canproduce different results.
Function Input and Output
The most useful functions work like this:
- Parameters - some values are sent to the function
- Arguments - some values are received by the function
- Function Code - some work is done inside the function
- Return Output - some value is returned from the function
In the next chapters, you will learn more aboutinput andreturn values.
Next Chapter
- Functions areexecuted when they arecalled orinvoked
- You call a function by addingparentheses to its name like:name()

