3 - Functions Flashcards

1
Q

What purposes do functions serve?

A
  1. reduce repetition
  2. enable naming concepts (abstractions)
  3. isolate code
  4. design and sturcture programs
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a function definition?

A

A binding that references a function value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What construct enables a function to receive input?

A

Parameters.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a function argument?

A

Value that is bind to a function parameter when the function is called.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a function body?

A

Block of code which wraps code of a function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How many params can a function have?

A

There is no limit on the number of params function can take.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What happens when function defines one param, but is passed two?

A

Additional parameters are ignored.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Why can’t a function be called with more parameters then the amount of the ones you define?

A

It can.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

When you invoke a function with lesser number of arguments than those defined, what happens?

A

Params which don’t receive a value on invocation are assigned ‘undefined’.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the minimal number of arguments you can define a function with?

A

Zero.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the ways to define a function?

A
  1. function foo() {}
  2. const foo = function() {}
  3. const foo = () => {}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

For which two things you use functions for?

A
  1. return a value
  2. side effect
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What keyword do you use inside a function to return a value?

A

‘return’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the return value when there is no ‘return’ statement in the function?

A

‘undefined’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What happens when you return two values in a list ‘return 1, 2;’?

A

Returned value is ‘2’.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the anatomy of a function?

A
  1. might have ‘function’ keyword
  2. name of a function
  3. parameters
  4. body
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

When you pass an array to a function, how do you assign its elements to different arguments?

A

Destructuring an array in the params. (function foo([a, b]))

18
Q

How is a top-level scope called?

A

Global scope.

19
Q

Which type of binding leaks out of a code block scope?

A

var

20
Q

Which type of binding is limited to the scope of the code block?

A

let/const

21
Q

What is the name of a scope created by a function?

A

Local.

22
Q

Why can you reference binding defined in the local (child) scope in the parent scope?

A

You can’t.

23
Q

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?

A

Global and local scope. Both will be available in the local scope.

24
Q

Does lexical scoping imply which bindings in the outer scope are visible in the local scopes, or the other way around?

A

It means that bindings defined in outer scopes are available in the nested (local) scopes.

25
Q

What is scope locality?

A

Degree of scope nesting.

26
Q

How deep can local scopes be nested?

A

Until stack overflow.

27
Q

What does it mean that a function is a value? How is it different from other languages?

A

1) it can be returned as a value of another function
2) it can be passed as a parameter to another function

28
Q

How do you write a function declaration?

A

‘function foo() {}’

29
Q

Why is function declaration different than assigning a function value to a binding?

A

Its binding can be used in the program before the location of the function declaration - hoisting.

30
Q

When defining arrow function, in which instances can you leave out parantheses?

A

When the function has one param.

31
Q

When defining arrow function, in which instances do you have to use parantheses?

A

1) when it doesn’t have any params
2) when it has two, or more, params

32
Q

When are you allowed to omit braces in the arrow function declaration?

A

When it is defined on a single line.

33
Q

Why do you have to use ‘return’ keyword when you define an arrow function on a single line?

A

You don’t.

34
Q

When defining arrow function on multiple lines how do you return a value?

A

With ‘return’ keyword.

35
Q

Why have arrow functions been added to the language?

A

Because it is convenient to write them when passing them inline as values.

35
Q

What is needed for a JS engine to be able to jump back and forth between different scopes?

A

Stack which is persisted in the computer memory.

35
Q

What is the flow of control here?

function greet(who) {
console.log(who);
}
greet(‘Harry’);
console.log(‘Hi’);

A

not in function
in greet
in console.log
in greet
not in function
in console.log
not in function

36
Q

How is the stack in which JS engine persists scopes/function calls called?

A

Call stack.

37
Q

What is stack overflow?

A

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.

38
Q

How can you intentionally blow up a stack?

A

1) calling a function recursively without a return
2) writing a circular reference
3) writing an infinite loop

39
Q

How do you define an optional parameter?

A

By writing ‘=’ after a param and assigning it a default value.

40
Q

What happens when function takes one optional parameter but is passed a value when invoked?

A

The parameter is bind to the passed value.