Unit 3: Programming · Procedures

Article

Defining and calling a procedure

6 min read

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.

AP pseudocode
PROCEDURE showMenu()
{
    DISPLAY("1. Play")
    DISPLAY("2. Scores")
    DISPLAY("3. Quit")
}
showMenu()
Runs in your browser

Each definition has four parts:

PartAP pseudocodePython
A word that starts the definitionPROCEDUREdef
The procedure's nameshowMenushowMenu
Brackets after the name()()
The body, the instructions insidebetween { 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?

AP pseudocode
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:

AP pseudocode
PROCEDURE showTitle()
{
    DISPLAY("=== Game ===")
}
PROCEDURE showMenu()
{
    showTitle()
    DISPLAY("1. Play")
    DISPLAY("2. Quit")
}
showMenu()
Runs in your browser

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?

AP pseudocode
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.

AP pseudocode
PROCEDURE showBorder()
{
    DISPLAY("*****")
}
showBorder()
DISPLAY("Hi")
showBorder()
Runs in your browser

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:

Runs in your browser
Runs in your browser

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 def line 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 PROCEDURE in Python, or def in 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.

AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse, this site. All lessons and questions here are written by JrCodex.