Activities 1.21 – 1.23. Use this to review, or to fill in something you missed.
function MeansA named piece of work. You had been typing function love.draw() since session 2 without being told.
love.draw is a name the engine looks for and runs on its own. A function you invent is a name the engine has never heard of, so your program has to call it.
Writing a function down does nothing at all. A program with twenty definitions and no calls runs perfectly and draws nothing.
Lua reads a file top to bottom, so a function has to be defined above the place it is used. Calling one that does not exist yet gives attempt to call global 'x' (a nil value) — the same message as a misspelled variable, for the same reason.
Lua does not count your arguments. Too few and the missing parameter is nil, which fails later and inside the function. Too many and the extras are silently discarded.
return hands a value back and ends the function immediately. The call turns into that value, right where it was written, so it can go anywhere a value goes — including inside another call.
returnRETURN(expression) on the exam, with parentheses.A function with no return hands back nil. Printing and returning are different jobs, and confusing them is the most common mistake in this part.
You had used return values all year without a name for them — tonumber, math.random, love.keyboard.isDown.
A local made inside a function exists only inside it. That is a feature: two functions can both use total and never collide.
Changing a parameter does not change the variable that was passed in. To actually update it, catch the returned value: score = bump(score).
Inside a drawing function, every measurement should be worked out from the parameters — size * 0.7 rather than 28.
One hardcoded number left behind is invisible at exactly one size — the size it was written for. That is the size the author tests at, which is why the bug survives.
The Create Performance Task requires at least one student-developed procedure with one or more parameters, with sequencing, selection and iteration in its body, and calls to it.
The College Board's wording: "Event handlers are built-in abstractions in some languages and will therefore not be considered student-developed."
| Function | Who calls it | Counts? |
|---|---|---|
love.draw | The engine | No |
love.update | The engine | No |
love.keypressed | The engine | No |
drawShip | You | Yes |
This is not a reason to avoid callbacks. You need love.draw; it is how anything appears at all. The rule is about what you point at in May, not how to write a program.