Unit 3: Programming · Procedures

Article

What a procedure is

5 min read

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.

AP pseudocode
PROCEDURE greet()
{
    DISPLAY("Hello!")
    DISPLAY("Welcome to class.")
}
greet()
greet()
Runs in your browser

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

  1. The program reaches a call

    for example greet()

    jump
  2. It goes into the procedure

    run
  3. The procedure's instructions run

    from top to bottom

    finish
  4. The program comes back

    to the line after the call

A call interrupts the normal order. When the procedure ends, the program carries on right 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?

AP pseudocode
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() and checkWinner().

Worked example

AP pseudocode
PROCEDURE greet()
{
    DISPLAY("Hello!")
    DISPLAY("Welcome to class.")
}
DISPLAY("Start")
greet()
DISPLAY("End")

Trace it. The definition is skipped over, because defining runs nothing.

StepWhat runsShown
1DISPLAY("Start")Start
2the call greet() jumps into the procedure
3DISPLAY("Hello!")Hello!
4DISPLAY("Welcome to class.")Welcome to class.
5the procedure ends, so the program returns to the line after the call
6DISPLAY("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, greet on 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.

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.