Article
What a procedure is
On your phone you press one button, "Call Mum". You do not tell the phone to find the number, dial it and wait for a reply. One name stands for many steps. Programmers do the same thing with a procedure.
A named group of instructions
A procedure is a group of instructions with a name. You write the instructions once. Then you use the name whenever you want those instructions to run. Other languages say "function" or "method". The AP exam always says procedure.
Here is a procedure named greet. It has two jobs: show a hello and show a welcome. First in AP pseudocode, which is what the exam shows. Then in Python, which you can run and change.
PROCEDURE greet()
{
DISPLAY("Hello!")
DISPLAY("Welcome to class.")
}
greet()
greet()The output is Hello! and Welcome to class., and then the same two messages again. The last two lines each call the procedure. A call means "run this procedure now".
Defining and calling are different
Two different things are happening in that program:
- Defining a procedure means writing its name and its instructions. Nothing runs yet. It is like writing a recipe on a card.
- Calling a procedure means using its name to run it. It is like cooking from the card.
A call is the name followed by brackets: greet(). The brackets are needed even when there is nothing inside them. Try deleting the two calls from the Python program above and run it again. You get no output, because the recipe was never used.
Check your understanding
What does a procedure definition do on its own?
Where the computer goes
Usually a program runs from the top line to the bottom line. A call changes that:
What happens at a call
The program reaches a call
for example greet()
jumpIt goes into the procedure
runThe procedure's instructions run
from top to bottom
finishThe program comes back
to the line after the call
The program does not forget where it was. It returns to the line directly after the call.
Check your understanding
What is displayed as a result of running this program?
PROCEDURE sayHi()
{
DISPLAY("Hi")
}
DISPLAY("A")
sayHi()
DISPLAY("B")Why use procedures?
- Write once, use many times. You do not copy and paste the same lines again and again.
- Fix once. If the greeting needs a change, you change it in one place.
- A name explains the code.
greet()tells a reader what happens without showing every step. - Big problems become small ones. A game can be built from
showMenu(),rollDice()andcheckWinner().
Worked example
PROCEDURE greet()
{
DISPLAY("Hello!")
DISPLAY("Welcome to class.")
}
DISPLAY("Start")
greet()
DISPLAY("End")Trace it. The definition is skipped over, because defining runs nothing.
| Step | What runs | Shown |
|---|---|---|
| 1 | DISPLAY("Start") | Start |
| 2 | the call greet() jumps into the procedure | |
| 3 | DISPLAY("Hello!") | Hello! |
| 4 | DISPLAY("Welcome to class.") | Welcome to class. |
| 5 | the procedure ends, so the program returns to the line after the call | |
| 6 | DISPLAY("End") | End |
The program shows Start, Hello!, Welcome to class., End, in that order.
Common mistakes
- Thinking the definition runs the procedure. Defining only stores the instructions. Nothing happens until a call.
- Forgetting the brackets in a call.
greet()runs the procedure. In Python,greeton its own does nothing. - Reading the procedure's lines when you reach the definition. When tracing, skip the definition and come back to it at a call.
- Losing your place after a call. The program continues on the line after the call, not at the top.
On the exam
You are shown a program with a procedure and its calls, and you say what is displayed. The order of the output depends on where the calls are, not where the definition is. A procedure that is defined but never called displays nothing.
Key words
Procedure: a named group of programming instructions.
Define: write a procedure's name and instructions.
Call: use a procedure's name to run it.