Back

Section Summary: Functions

divider

Activities 1.21 – 1.23. Use this to review, or to fill in something you missed.


1. What function Means

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

Function
A named piece of work. The exam calls it a procedure.
Parameter
A blank in the description, named in the definition.
Argument
The value you put in the blank when you call it.
Call
Saying the name with parentheses, to make it run.

2. Defining Is Not Calling

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.


3. Functions That Answer

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.

return
Hands a value back. RETURN(expression) on the exam, with parentheses.
Return value
What the call becomes.

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.


4. Scope

A local made inside a function exists only inside it. That is a feature: two functions can both use total and never collide.

Scope
Where a name exists.
Shadowing
An inner name hiding an outer one spelled the same. The parameter is its own variable.

Changing a parameter does not change the variable that was passed in. To actually update it, catch the returned value: score = bump(score).


5. Scaling, and Moving as One Piece

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.


6. Which Procedures Count in May

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

FunctionWho calls itCounts?
love.drawThe engineNo
love.updateThe engineNo
love.keypressedThe engineNo
drawShipYouYes

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.

Callback
A function the engine calls for you, at a moment it decides.
Event handler
A callback that runs in response to something happening. The College Board's word.
Student-developed procedure
A function you wrote and call yourself.