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?
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?
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.
5.[3p] Which of these functions are impure?
Which of these functions are impure?
Select all that apply
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?