AP® Computer Science Principles · Big Idea 3 · Topic 3.12

Return Values and the Flow of Control

When code calls a procedure, control jumps to that procedure, runs it, then jumps back to the exact spot it left. A RETURN statement is what hands a usable value back to the caller.

A procedure call pauses the line of code that's running. Control leaves the caller, jumps into the procedure, and runs every instruction inside it. If the procedure hits a RETURN statement, control immediately jumps back to the exact spot in the caller where the call happened, carrying the returned value with it. The caller then treats that call as if it were just the value itself, and finishes executing its own line.

This matters because it's the only way a procedure can hand back something useful. A procedure that runs DISPLAY shows a value on the screen, but that's a side effect, not a handoff. The caller gets nothing to store, compare, or compute with. Only an explicit RETURN gives the caller a value it can plug into an expression, pass to another procedure, or save in a variable.

RETURN also ends the procedure the moment it executes, even if more lines sit below it. That's useful for branching logic: a procedure can check a condition early, RETURN one value if it's true, and only run the remaining code if it's false. This flow of control connects directly to how lists and loops call procedures repeatedly, and sets up why procedures with no RETURN are used only for their side effects, like displaying or changing state, never for their output.

Key terms

Procedure call
An instruction that pauses the current line and transfers control into a named procedure.
RETURN statement
A statement that sends a value back to the caller and immediately ends the procedure.
Flow of control
The order in which instructions actually execute, including jumps into and out of procedures.
Side effect
An action like displaying text or changing stored data that a procedure performs without returning a value.

Watch it explained

  1. How it worksWalk me through control leaving the caller, running the procedure, and returning to the same spot.
  2. Worked exampleTrace a program calling a doubling procedure inside a display statement, for the input 12.
  3. Going furtherWhat changes when a RETURN statement sits in the middle of a procedure instead of at its end?
  4. Common mistakeWhy doesn't a procedure that only displays a value give the caller anything to work with?

Check yourself

Your turn1 of 2
A procedure calls a doubling procedure inside a display statement, for the input 70. What value gets displayed?

End of topic.