Functions Flashcards
What does a regular function definition look like?
What does line 6 do?
the code say() is described as a function call to the say function. When JavaScript runs this program, it creates a function named say whose body causes JavaScript to print the text Output from say() when the function executes.
Note that the parentheses on line 6 – () – make this code a function call. Without the parentheses, say doesn’t do anything useful. It’s just the function’s name; it is a variable whose value is a “function object.”
Is console.log also a function call?
It is, in fact, a function call, though we typically call it a method call instead. The . in console.log distinguishes it as a method call instead of a function call.
What do arguments do?
Arguments let you pass data from outside the function’s scope into the function so it can access the data. You don’t need arguments if the function definition doesn’t need access to outside data.
What are parameters?
In the definition of a function, the names between parentheses are called parameters. You can think of parameters as placeholders, while arguments refer to the values assigned to those placeholders.
In say.js the code within the function definition then executes with the text local variable assigned to “hello”.
What are function names and parameters considered in JS?
Function names and parameters are both considered variable names in JavaScript. Parameters, in particular, are local variables (the scope of the variable defined by the parameter is the function definition; you can’t use it outside the function’s body). They are defined locally within the function’s body. Function names are either global or local, depending on whether the function is at the program’s top level or nested inside a class, object, or another function.
Difference between arguments and parameters?
Arguments are objects or primitive values being passed to the function; parameters are declarations for the local variables used inside the function to access the arguments.
What happens if you provide too many or too few arguments?
The additional arguments will be ignored if you provide too many arguments when calling a function. If you provide too few arguments, the parameters and variables that correspond to the missing arguments will receive a value of undefined:
What is the implicit return value of most JS functions?
Undefined
What is an explicit return value?
When you use a return statement, you can return a specific value from a function.
What happens when JS encounters the ‘return’ statement?
It evaluates the expression, terminates the function, and returns the expression’s value to the location where we called it.
What is the calling function sometimes referred as?
The caller
What are functions that return a boolean value called?
Predicates
How does a function with default parameters look like?
When you define a function, you sometimes want to structure it so you can call it without an argument
Nested functions
You can create functions anywhere, even nested inside other functions.
Nested functions get created and destroyed every time the outer function runs. (This usually has a negligible effect on performance.)
Quick review: difference between global variables vs local variables.
Global variables are available throughout a program, while local variables are confined to a function or a block.
Can we reassign global variables from within a function?
Yes!
Line 11 invokes the function, passing in the ‘Good Evening’ string, which becomes the new value for the global greetingMessage.