Functions Flashcards
What is a function?
a function is a procedure that lets you extract code and run it as a separate unit
what is a function name without parentheses?
Without parentheses a function like funcName() is not a function call but simply the name of the function. The value being a function object representing the function in question
what scope do parameters have?
Parameters arelocal variablesonly available within the function’s body
What happens when you nest a function in another function?
These are private functions which can only be called within the parent functions definition.
What scope do function names have?
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.
What are arguments?
Arguments are objects or primitive values passed to a function
What are parameters?
parameters are declarations for the local variables used inside the function to access the arguments.
What happens if you provide too few/too many arguments?
If you provide too few arguments, the parameters and variables that correspond to the missing arguments will receive a value ofundefined. Additional arguments will be ignored if you provide too many
What is a function which only returns a boolean called?
A predicate
What is the implicit return value of most JS functions?
Undefined
What is an explicit return value?
An explicit return value is when you provide an specific overriding return in the function definition
how do you define default parameters?
function say(text = “hello”) {
console.log(text + “!”);
}
How is a method different than a function? How can we tell the difference?
Methods are functions built into existing objects. We can tell because they use a period for invocation. ‘xyzzy’.toUpperCase()
What’s the difference between reassignment and mutation?
To change a variable can either change what value is assigned (orbound) to a variable or we can change the value of the thing that is bound to the variable. The former is known asreassignmentwhile the latter is known asmutation.
Can we change primitives?
No. Primitive values are immutable.
What is function composition?
Function composition is when we use a function call as an argument to another function. Because all function calls evaluate to a value, this is the same as passing the return value of the second function to the first.
What are the three ways of defining a function?
Function declaration,
Function expressions, and arrow functions
which methods of function definition allow you to call a function before it is defined.
Only function declaration
What is function declaration?
function functionName(zeroOrMoreArguments…) {
// function body
}
What is a function expression?
let greetPeople = function () {
console.log(“Good Morning!”);
};
Any function definition that doesn’t have the word function at the very beginning is a function expression.
What does it mean that JS functions are ‘first-class-functions’?
This means that JS treats functions like any other variable. Which in fact they are, all JavaScript functions are objects
how do you define an arrow function?
let add = (a, b) => a + b;
Can we omit return statements in arrow functions?
We can omit return statements in arrow functionswhen and only when the function body contains a single expression that is not itself surrounded by curly braces
Can parameter parentheses can only be omitted in arrow functions?
parameter parentheses can only be omitted if the function has a single simple parameter
What is the call stack?
the call stack is a mechanism for an interpreter (like the JavaScript interpreter in a web browser) to keep track of its place in a script that calls multiple functions — what function is currently being run and what functions are called from within that function, etc.
What order does JS add/remove frames from the call stack
the call stack puts information about the current function on the top of the stack, then removes that information when the function returns.
What are the units on the call stack called?
Stack Frames
What is the initial stack frame referred to as?
The main function
What is a limitation of recursive functions?
recursive functions can break the call stack by exceeding the call stack length if they keep calling themselves too many times.