Sign in

Libre University uses your GitHub account. Signing in is only needed to sit a final test, so the score is kept on your profile.

Functions

1.[2p]

What does a function return if its body has no return statement?

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

CorrectNot quite: undefined

3.[2p]

What is the difference between console.log(x) and return x?

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

Correct
The answer is: True

5.[3p]

Which of these functions are impure?

Select all that apply

Correct
Correct
Correct
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?

CorrectNot quite: 25

7.[2p]

let n = 1; function g() { let n = 2; return n; } What does g() + n evaluate to?

CorrectNot quite: 3

8.[3p]

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?

Correct
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`