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.