Functions Flashcards

1
Q

What does a regular function definition look like?

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

What does line 6 do?

A

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.”

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

Is console.log also a function call?

A

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.

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

What do arguments do?

A

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.

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

What are parameters?

A

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”.

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

What are function names and parameters considered in JS?

A

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.

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

Difference between arguments and parameters?

A

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.

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

What happens if you provide too many or too few arguments?

A

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:

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

What is the implicit return value of most JS functions?

A

Undefined

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

What is an explicit return value?

A

When you use a return statement, you can return a specific value from a function.

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

What happens when JS encounters the ‘return’ statement?

A

It evaluates the expression, terminates the function, and returns the expression’s value to the location where we called it.

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

What is the calling function sometimes referred as?

A

The caller

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

What are functions that return a boolean value called?

A

Predicates

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

How does a function with default parameters look like?

A

When you define a function, you sometimes want to structure it so you can call it without an argument

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

Nested functions

A

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.)

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

Quick review: difference between global variables vs local variables.

A

Global variables are available throughout a program, while local variables are confined to a function or a block.

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

Can we reassign global variables from within a function?

A

Yes!

Line 11 invokes the function, passing in the ‘Good Evening’ string, which becomes the new value for the global greetingMessage.

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

Local variables

A

The greetPeople function declares greetingMessage internally. It’s accessible within the function, but a ReferenceError occurs if you try to use it elsewhere.

19
Q

How can we ask greetPeople to output a different greeting message if we’ve hard-coded our greeting message on line 2?

A

With function arguments:

20
Q

Are parameters a local variable?

A

It is, in fact, a local variable. The chief difference is that we initialize it from the argument passed to the function. Parameters have local scope within a function.

21
Q

What is a very important property of local variables?

A

Local variables are short-lived; they go away when the function corresponding to their scope stops running. When we invoke the function, we start a new scope. If the code within that scope declares a new variable, that variable belongs to the scope. When the last bit of code in that scope finishes running, the locally scoped variables disappear. JavaScript repeats this process each time we invoke a function.

22
Q

How do you also confine a variable locally?

A

Through block-scoping.

Block scoping occurs when you use let or const inside a block and confines the variable’s scope to that block.

23
Q

What are function methods?

A

Method invocation occurs when you prepend a variable name or value followed by a period (.) to a function invocation, e.g., ‘xyzzy’.toUpperCase().

24
Q

What is reassignment?

A

It changes what value is assigned (or bound) to a variable.

Reassignment simply makes that name (variable) refer to a completely different place in memory and, hence, a different value.

We sometimes describe reassignment as changing the binding of a variable

25
Q

What is mutation?

A

It changes the value of the thing that is bound to the variable.

It changes the value that is actually stored in the memory that the name refers to. After mutating the value assigned to a variable, the variable continues to refer to the same place in memory.

26
Q

If the variable refers to an array or an object, would reassigning an element reassign the variable?

A

No!

It mutates that array or object referenced by that variable.

27
Q

Are primitive values mutable?

A

No! They are immutable.

Primitive values are immutable. That means their values never change; operations on immutable values always return new values.

28
Q

What values are mutable?

A

Operations on mutable values (arrays and objects) may or may not return a new value and may or may not mutate data.

There’s no way to tell whether a function mutates an array or object. You have to use the documentation or memorize it through repetition.

29
Q

Is JS a pass-by-value or pass-by-reference language?

A

JavaScript is both! It uses pass-by-value when dealing with primitive values and pass-by-reference with objects and arrays.

30
Q

What is function composition?

A

JavaScript lets us use a function call as an argument to another function.

Life gets more interesting when you pass function call results to a function that does something more complicated:

31
Q

Function declaration

A

In JavaScript, we call a function definition that looks like that a function declaration. A notable property of function declarations is that you can call the function before you declare it.

32
Q

Function expressions

A

Most of it seems like a standard function declaration. However, since we’re saving it to a variable, it’s a function expression instead. Function expressions have one key difference from a function declaration: you cannot invoke a function expression before it appears in your program.

Our example declares a variable named greetPeople and assigns it to the function expression after the = sign. We can do that since JavaScript functions are first-class functions.

33
Q

What is the critical feature of first-class-functions?

A

You can treat them like any other value. In fact, all JavaScript functions are objects. Thus, you can assign them to variables, pass them as arguments to other functions, and return them from a function call.

34
Q

Why a function declaration must start with the word ‘function’?

A

Any function definition that doesn’t have the word function at the very beginning of a statement is a function expression. Even wrapping what looks like a function declaration in parentheses creates a function expression:

35
Q

Another common example of function expressions in higher order functions

A

That is not a function declaration – it’s a function expression.

36
Q

Arrow function syntax

A
37
Q

What are implicit returns in arrow functions?

A

We can omit it in arrow functions when and only when the function body contains a single expression that is not itself surrounded by curly braces (the expression may have subexpressions, but the entire expression must evaluate to a single value). Suppose it contains two or more expressions or statements. In that case, you must explicitly return a value if you need it, and you must also use curly braces

38
Q

What is the call stack?

A

The call stack helps JavaScript keep track of what function is executing as well as where execution should resume when the function returns

39
Q

What is the stack frame?

A

When this program starts running, the call stack initially has one item – called a stack frame – that represents the global (top-level) portion of the program. The initial stack frame is sometimes called the main function. JavaScript uses this frame to keep track of what part of the main program it is currently working on.

40
Q

What happens when the program execution reaches line 10?

A

When program execution reaches the function invocation on line 10, it first updates the main stack frame with the current program location. JavaScript will use this location later to determine where execution should resume when second finishes running.

41
Q

What happens after setting the location in the current stack frame for the function call second()

A

JavaScript creates a new stack frame for the second function and places it on the top of the call stack: we say that the new frame is pushed onto the stack. Our call stack now looks like this:

Note that the frame for the second function is now stacked on top of the main frame. While the second frame is still on the stack, main remains stuck beneath it, inaccessible. At the same time, the main function becomes dormant and the second function becomes active.

42
Q

What happens in the stack when a value is returned?

A

When a value is returned, JavaScript removes – pops – the top frame from the call stack. That’s the frame for console.log in this example. That leaves the previous stack frame exposed. JavaScript uses this frame to determine where execution should resume. In this case, execution resumes immediately after line 2.

43
Q

What happens when the main function has no more code to run?

A

The main frame gets popped from the stack, and the program ends.

44
Q

Does the stack have limited size?

A

The call stack has a limited size that varies based on the JavaScript implementation. That size is usually sufficient for more than 10000 stack entries. If the stack runs out of room, you will see a RangeError exception together with a message that mentions the stack.