AP Computer Science Principles

Unit 3.3 – Procedures and Functions (High School Notes)

Big Idea

What is a Procedure?

A procedure (also called a function) is a named set of instructions that performs a specific task. Instead of rewriting the same code multiple times, you define it once and call it whenever needed.

Why Procedures Matter in AP CSP

The College Board expects you to be able to:

Procedures help with:

Defining a Procedure

When you define a procedure, you create it.

General Pseudocode Format

PROCEDURE procedureName(parameter1, parameter2)
{
    statements
}

Example (No Return Value)

PROCEDURE greetUser(name)
{
    DISPLAY "Hello " + name
}

This procedure:

Calling a Procedure

When you call a procedure, you tell the program to run it.

Example

greetUser("Joe")

Output:

Hello Joe

Parameters

A parameter is a variable used in a procedure definition that receives a value. Think of it like a placeholder.

Example

PROCEDURE square(number)
{
    DISPLAY number * number
}

Calling it:

square(5)

The value 5 is passed into the procedure.

Arguments vs Parameters

Term Meaning
Parameter Variable in the procedure definition
Argument Actual value passed when calling

Example:

square(5)

Returning Values

Some procedures return a value instead of just displaying something. Returning is useful when you want to store or reuse the result later.

Example with Return

PROCEDURE add(a, b)
{
    RETURN a + b
}

Calling it:

result ← add(3, 4)
DISPLAY result

Output:

7
Important: A procedure that returns a value does not automatically display it. You must store it or display it yourself.

Modular Design

Modular design means breaking a large program into smaller, manageable pieces (modules). Each module should have one main job.

Without Modular Design (Messy)

Ask for name
Print greeting
Ask for score
Calculate grade
Print grade

With Modular Design (Clean)

getName()
printGreeting()
calculateGrade()
printGrade()

Benefits:

Managing Complexity

Procedures help manage complexity by hiding implementation details. Instead of worrying about how something works, you can focus on the big picture.

calculateGrade(score)

You trust the procedure handles the details correctly.

AP CSP Create Task Connection

For the Create Performance Task, you must:

Example AP-Level Procedure

PROCEDURE calculateDiscount(price, isMember)
{
    IF isMember = true
    {
        RETURN price * 0.8
    }
    ELSE
    {
        RETURN price
    }
}

This includes:

Common Mistakes Students Make

Quick Check Questions

  1. What is the difference between defining and calling a procedure?
  2. What is a parameter?
  3. Why is modular design important?
  4. When should you return a value instead of displaying it?
  5. How do procedures help manage complexity?
Big Idea Summary: Procedures and functions allow you to break problems into smaller pieces, reuse code, manage complexity, and write cleaner programs—plus they help you meet AP CSP Create Task requirements.