3 - Functions Flashcards
What purposes do functions serve?
- reduce repetition
- enable naming concepts (abstractions)
- isolate code
- design and sturcture programs
What is a function definition?
A binding that references a function value.
What construct enables a function to receive input?
Parameters.
What is a function argument?
Value that is bind to a function parameter when the function is called.
What is a function body?
Block of code which wraps code of a function.
How many params can a function have?
There is no limit on the number of params function can take.
What happens when function defines one param, but is passed two?
Additional parameters are ignored.
Why can’t a function be called with more parameters then the amount of the ones you define?
It can.
When you invoke a function with lesser number of arguments than those defined, what happens?
Params which don’t receive a value on invocation are assigned ‘undefined’.
What is the minimal number of arguments you can define a function with?
Zero.
What are the ways to define a function?
- function foo() {}
- const foo = function() {}
- const foo = () => {}
For which two things you use functions for?
- return a value
- side effect
What keyword do you use inside a function to return a value?
‘return’
What is the return value when there is no ‘return’ statement in the function?
‘undefined’
What happens when you return two values in a list ‘return 1, 2;’?
Returned value is ‘2’.
What is the anatomy of a function?
- might have ‘function’ keyword
- name of a function
- parameters
- body
When you pass an array to a function, how do you assign its elements to different arguments?
Destructuring an array in the params. (function foo([a, b]))
How is a top-level scope called?
Global scope.
Which type of binding leaks out of a code block scope?
var
Which type of binding is limited to the scope of the code block?
let/const
What is the name of a scope created by a function?
Local.
Why can you reference binding defined in the local (child) scope in the parent scope?
You can’t.
If a function is defined in the global scope, where can you create a binding and have it available in the local scope of a function?
Global and local scope. Both will be available in the local scope.
Does lexical scoping imply which bindings in the outer scope are visible in the local scopes, or the other way around?
It means that bindings defined in outer scopes are available in the nested (local) scopes.
What is scope locality?
Degree of scope nesting.
How deep can local scopes be nested?
Until stack overflow.
What does it mean that a function is a value? How is it different from other languages?
1) it can be returned as a value of another function
2) it can be passed as a parameter to another function
How do you write a function declaration?
‘function foo() {}’
Why is function declaration different than assigning a function value to a binding?
Its binding can be used in the program before the location of the function declaration - hoisting.
When defining arrow function, in which instances can you leave out parantheses?
When the function has one param.
When defining arrow function, in which instances do you have to use parantheses?
1) when it doesn’t have any params
2) when it has two, or more, params
When are you allowed to omit braces in the arrow function declaration?
When it is defined on a single line.
Why do you have to use ‘return’ keyword when you define an arrow function on a single line?
You don’t.
When defining arrow function on multiple lines how do you return a value?
With ‘return’ keyword.
Why have arrow functions been added to the language?
Because it is convenient to write them when passing them inline as values.
What is needed for a JS engine to be able to jump back and forth between different scopes?
Stack which is persisted in the computer memory.
What is the flow of control here?
function greet(who) {
console.log(who);
}
greet(‘Harry’);
console.log(‘Hi’);
not in function
in greet
in console.log
in greet
not in function
in console.log
not in function
How is the stack in which JS engine persists scopes/function calls called?
Call stack.
What is stack overflow?
An event where the memory of the call stack is full, but the program continues to add new contexts to it, resulting in an error.
How can you intentionally blow up a stack?
1) calling a function recursively without a return
2) writing a circular reference
3) writing an infinite loop
How do you define an optional parameter?
By writing ‘=’ after a param and assigning it a default value.
What happens when function takes one optional parameter but is passed a value when invoked?
The parameter is bind to the passed value.