Article
Defining and calling a procedure
You know what a procedure is. Now you will write one. The shape is the same every time, so once you learn it, you can write any procedure and read any procedure the exam shows you.
The parts of a definition
Here is a procedure that shows a small menu. The AP version comes first, then the Python version.
PROCEDURE showMenu()
{
DISPLAY("1. Play")
DISPLAY("2. Scores")
DISPLAY("3. Quit")
}
showMenu()Each definition has four parts:
| Part | AP pseudocode | Python |
|---|---|---|
| A word that starts the definition | PROCEDURE | def |
| The procedure's name | showMenu | showMenu |
| Brackets after the name | () | () |
| The body, the instructions inside | between { and } | indented lines after a : |
The body is everything the procedure does. In AP pseudocode the braces mark where it starts and ends. In Python the indentation (the spaces at the start of each line) does the same job. The first line that is not indented again is outside the procedure. That line is where the program carries on.
A good name says what the procedure does: showMenu, rollDice, playSound. A name has no spaces, just like a variable name.
Check your understanding
Which line of this program is a call to a procedure?
PROCEDURE showScore()
{
DISPLAY("Score")
}
score ← 10
showScore()Calling, in any order, as often as you like
You call a procedure by writing its name and brackets. You can call it once, ten times, or never. You can also call it from inside another procedure:
PROCEDURE showTitle()
{
DISPLAY("=== Game ===")
}
PROCEDURE showMenu()
{
showTitle()
DISPLAY("1. Play")
DISPLAY("2. Quit")
}
showMenu()The program calls showMenu(). Inside it, the first line calls showTitle(). That procedure finishes, and then showMenu carries on with its second line. The output is === Game ===, 1. Play, 2. Quit.
Python reads from the top, so a procedure must be defined before the line that calls it runs. Put your definitions at the top. The AP exam also shows definitions first.
Check your understanding
What is displayed as a result of running this program?
PROCEDURE inner()
{
DISPLAY("middle")
}
PROCEDURE outer()
{
DISPLAY("start")
inner()
DISPLAY("end")
}
outer()Worked example
Write a procedure showBorder that displays a line of stars. Call it before and after a message.
PROCEDURE showBorder()
{
DISPLAY("*****")
}
showBorder()
DISPLAY("Hi")
showBorder()The output is *****, Hi, *****. The stars are written once and used twice. If you want a different border, change one line.
In Python, watch what the indentation decides. Compare these two programs:
The first one displays Hi and then *****. The print("Hi") line is not indented, so it is outside the procedure and it runs straight away. In the second one, print("Hi") is indented, so it is inside the body. It runs only when the procedure is called, and the output is ***** then Hi.
Common mistakes
- Forgetting the brackets after the name, in the definition or in the call.
- Missing colon or bad indentation in Python. A
defline ends with:, and the body must be indented. - Calling a procedure before defining it in Python. You get a
NameError. - Defining a procedure but never calling it. Nothing is displayed.
- Putting a line inside the body by accident, or leaving one outside by accident. Check the braces or the indentation.
- Writing
PROCEDUREin Python, ordefin AP pseudocode. Each language has its own word.
On the exam
You read a definition and the calls that follow it, and you decide what is displayed. Find which lines belong to the body (between the braces), skip the definition on your first pass, and follow the calls in order. A procedure that calls another procedure is common, so remember that the program always comes back to the line after the call.
Key words
Body: the instructions inside a procedure's definition.
Procedure name: the word you write to call the procedure.
Indentation: the spaces at the start of a line. In Python they show which lines are in the body.