01 / 21

Calling
Procedures

Learn how to call procedures, pass arguments, and use return values to write modular, reusable code in any programming language.

AP Computer Science Principles

What Is a Procedure?

A procedure (also called a function or method) is a named group of programming instructions that performs a specific task. Instead of writing the same code over and over, you define it once and call it whenever you need it.

Define
Write the instructions once inside a named block
Call
Execute those instructions by using the procedure's name
Reuse
Call it as many times as needed throughout your program

Why Call Procedures?

1
Modularity: Break complex programs into smaller, manageable pieces that are easier to understand and debug.
2
Reusability: Write code once and call it from multiple places, eliminating duplication.
3
Abstraction: Hide complex details behind a simple name. The caller doesn't need to know how it works, just what it does.
4
Collaboration: Different team members can write different procedures, then combine them into one program.

Anatomy of a Procedure Call

moveForward ( 10 ) Procedure Name Argument Parentheses enclose arguments procedureName( argument1, argument2, ... )

Parameters vs Arguments

DEFINITION PROCEDURE greet(name) DISPLAY("Hello, " + name) name = parameter CALL greet("Alice") greet("Bob") "Alice", "Bob" = arguments Parameter = variable in the definition (placeholder) Argument = actual value passed when calling

The Call-and-Return Model

Main Program x ← 5 result ← square(x) DISPLAY(result) CALL PROCEDURE square(n) result ← n * n RETURN(result) n receives the value 5 result = 25 RETURN 25 result = 25

Return vs No Return

Returns a Value
Sends a result back to the caller. You can store it in a variable or use it in an expression.
PROCEDURE add(a, b)
  RETURN(a + b)

sum ← add(3, 7) // sum = 10
No Return Value
Performs an action (like displaying output) but does not send a value back. You just call it.
PROCEDURE sayHi(name)
  DISPLAY("Hi, " + name)

sayHi("Alice") // prints Hi, Alice

Defining vs Calling

Defining Calling
What it does Creates the procedure Executes the procedure
When it runs Does NOT run the code Runs the code immediately
How many times Defined once Called many times
Uses Parameters (placeholders) Arguments (actual values)
Syntax clue PROCEDURE keyword Just the name( )

How to Call a Procedure

1
Identify the procedure name. Look for the name that was defined with the PROCEDURE keyword.
2
Count the parameters. Check how many values the procedure expects. The number of arguments must match the number of parameters.
3
Provide the arguments. Pass actual values (or variables) in the correct order inside parentheses.
4
Use the return value (if any). If the procedure returns something, store it in a variable or use it directly in an expression.

Example 1: Simple Call

PROCEDURE double(x)
{
  RETURN(x * 2)
}

a ← 7
result ← double(a)
DISPLAY(result)

Example 2: Multiple Parameters

PROCEDURE calculateArea(length, width)
{
  area ← length * width
  RETURN(area)
}

roomArea ← calculateArea(12, 8)
DISPLAY("Area: " + roomArea)

Example 3: Nested Calls

PROCEDURE square(n)
{
  RETURN(n * n)
}

PROCEDURE sumOfSquares(a, b)
{
  RETURN(square(a) + square(b))
}

result ← sumOfSquares(3, 4)
DISPLAY(result)

Example 4: No Return Value

PROCEDURE drawStars(count)
{
  REPEAT count TIMES
  {
    DISPLAY("★")
  }
}

drawStars(5)

Execution Flow When Calling

1. Code runs normally top to bottom 2. Encounters call execution pauses here 3. Jumps to procedure arguments → parameters 4. Procedure runs its own code executes 5. RETURN (optional) sends value back 6. Resumes main code continues where it left off The caller pauses, the procedure executes, then control returns to exactly where it left off.

Common Mistakes

✕ Wrong Argument Count
Passing 2 arguments to a procedure that expects 3. The number of arguments must exactly match the number of parameters.
✕ Wrong Argument Order
Calling calculateArea(width, length) instead of calculateArea(length, width). Arguments map to parameters by position, not by name.
✕ Ignoring the Return Value
Calling add(3, 5) without storing the result. The return value is lost if you don't assign it to a variable.
✕ Storing a Non-Return
Writing x ← drawStars(5) when drawStars has no RETURN statement. This gives an undefined or error result.

Summary Table

Concept Definition Example
Procedure Named block of reusable code PROCEDURE greet(name)
Parameter Placeholder variable in definition name in greet(name)
Argument Actual value passed during call "Alice" in greet("Alice")
Return Value sent back to caller RETURN(result)
Calling Executing a defined procedure greet("Bob")

Question 1 of 5

What value is displayed?

Question 2 of 5

In a procedure definition, the variables listed inside the parentheses are called:

Question 3 of 5

What value is displayed?

Question 4 of 5

What is the value of result after this code runs?

Question 5 of 5

What is the value of result?