FUNCTIONS Flashcards
What is a function declaration ?
Is a function that is bound to an identifier, or name.
What is the anatomy of a function declaration?
- The function keyword.
- The name of the function, or its identifier, followed by parentheses.
- A function body, or the block of statements required to perform a specific task, enclosed in the function’s curly brackets, { }.
How to call a function ?
Functions can be called, or executed, elsewhere in code using parentheses following the function name. When a function is called, the code inside its function body runs. Arguments are values passed into a function when it is called.
How do we call the input when we declare a function ?
Parameters
What does parameters in a function?
Parameters allow functions to accept input(s) and perform a task using the input(s).
We use parameters as placeholders for information that will be passed to the function when it is called.
What to do when we are calling a function that has parameters?
We specify the values in the parentheses that follow the function name.
What do we call the values that are passed to the function?
The values that are passed to the function when it is called are called arguments. Arguments can be passed to the function as values or variables.
How to print the result of the function (with parameters)?
You need to use the keyword return.
> Also called : a return statement
Otherwise, the computer is going to calculate (or something else) but not capture it.
How to create a return statement?
> We use the return keyword followed by the value that we wish to return.
The return keyword is powerful because it allows functions to produce an output. We can then save the output to a variable for later use.
What is a helper functions ?
We can also use the return value of a function inside another function. These functions being called within another function are often referred to as helper functions. Since each function is carrying out a specific task, it makes our code easier to read and debug if necessary.
What is another way to define a function ?
Another way to define a function is to use a function expression.
How is called a function with no name?
A function with no name is called an anonymous function.
How to invoke a function expression?
Write the name of the variable in which the function is stored followed by parentheses enclosing any arguments being passed into the function.
What is different for the function expressions compared to function declarations?
Unlike function declarations, function expressions are not hoisted so they cannot be called before they are defined.