'F' → Fullscreen
Terminal today. Copy template-console.
Copy template-console to 1-22-return and write both:
local function shout(text) print(text .. "!")end
local result = shout("hi")print("result is " .. tostring(result))hi!result is nilThen write double from the slides and store its answer in a variable. Write down what each of the two functions gave back.
local function area(w, h) return w * hend
local kitchen = area(3, 4)local hall = area(2, 6)
print(kitchen + hall)24The two calls became numbers and then got added together. Now call area inside another call — try print(area(area(2, 3), 4)) and work out the answer before you run it.
Each one takes something and hands something back:
celsius(f) — turns Fahrenheit into CelsiusisEven(n) — hands back true or falsebiggest(a, b, c) — hands back the largest of threeisEven can be written with an if, and it can also be written in one line with no if at all. Find the second way if you can.
Run both of these and record the output:
local function makeIt() local hidden = 99 return hiddenend
print(makeIt())print("outside: " .. tostring(hidden))local score = 10
local function bump(score) score = score + 5 return scoreend
print(bump(score), score)The second one is the surprising one. The function changed score and the outer score did not move. Write down both numbers and explain them to the person next to you before you read on.
Write a function that computes something and forgets to return it:
local function noReturn(n) n * 2endError: Syntax error: main.lua:2: '=' expected near '*'This one will not even start. Lua reads n * 2 on its own and expects it to be the left side of an assignment. Copy the message down.
Then make the forgetful version that does run: put local answer = n * 2 in the function with no return, and use it. That one has no error at all, and it is the version you will actually write by accident.
Write minMax(a, b) from the slides, then write stats(t) that takes a table of numbers and hands back three things: the total, the count, and the average.
It is a traversal from session 20 with a return at the end.
The exam's procedures return values too, and the capture is written with the arrow you already know.
local function area(w, h) return w * hend
local kitchen = area(3, 4)PROCEDURE area(w, h){ RETURN(w * h)}
kitchen ← area(3, 4)| Lua | Exam notation | |
|---|---|---|
| Hand a value back | return w * h | RETURN(w * h) |
| Catch it | local kitchen = area(3, 4) | kitchen ← area(3, 4) |
RETURN takes parentheses and Lua's return does not. It also behaves the same way in both: it can appear anywhere, and it ends the procedure immediately.
PROCEDURE. Lua is the same — function either way — so the difference is entirely in whether the body says return.return hands backarea were added togetherreturn were runAnswer the following questions before submitting your work.
bump(score) returned 15 and the outer score stayed 10. Explain why, and say what you would have to write to actually change the outer one.return gave a syntax error and the other gave no error at all. Explain the difference between the two versions, and say which one you would rather write by mistake.Submit the required files to the appropriate dropbox.