Functions
1.[2p] What does a function return if its body has no return statement?
What does a function return if its body has no return statement?
The answer is: undefined
The answer is: undefined
The answer is: undefined
2.[2p] function area(w, h) { w * h; } What does area(3, 4) evaluate to? Write it in lower case.
function area(w, h) { w * h; } What does area(3, 4) evaluate to? Write it in lower case.
3.[2p] What is the difference between console.log(x) and return x?
What is the difference between console.log(x) and return x?
The answer is: console.log sends text to a screen for a human, while return hands a value back to the rest of the program
The answer is: console.log sends text to a screen for a human, while return hands a value back to the rest of the program
The answer is: console.log sends text to a screen for a human, while return hands a value back to the rest of the program
4.[1p] A function is called pure when its result depends only on its arguments and it has no effect on anything outside itself.
A function is called pure when its result depends only on its arguments and it has no effect on anything outside itself.
The answer is: True
5.[3p] Which of these functions are impure?
Which of these functions are impure?
Select all that apply
The answer is: One that calls Math.random(), One that calls console.log before returning, One that reads a let declared outside itself
6.[2p] function power(base, exponent = 2) { return base ** exponent; } What does power(5) return?
function power(base, exponent = 2) { return base ** exponent; } What does power(5) return?
7.[2p] let n = 1; function g() { let n = 2; return n; } What does g() + n evaluate to?
let n = 1; function g() { let n = 2; return n; } What does g() + n evaluate to?
8.[3p] Match each part of a function's contract to what it names.
Match each part of a function's contract to what it names.
Parameter
Argument
Precondition
Side effect
Return value
the single result handed back
anything the function changes outside itself
the restriction the caller must satisfy
the name used inside the function body
the value supplied at the call
Show the answer
Parameter: the name used inside the function body Argument: the value supplied at the call Precondition: the restriction the caller must satisfy Side effect: anything the function changes outside itself Return value: the single result handed back
9.[3p] Why is return on its own line, with the value on the line below, always wrong?
Why is return on its own line, with the value on the line below, always wrong?
The answer is: A semicolon is inserted after the bare return, so the expression below is never reached and the function returns undefined
The answer is: A semicolon is inserted after the bare return, so the expression below is never reached and the function returns undefined
The answer is: A semicolon is inserted after the bare return, so the expression below is never reached and the function returns undefined