Unit 3.3 – Procedures and Functions (High School Notes)
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.
The College Board expects you to be able to:
Procedures help with:
When you define a procedure, you create it.
PROCEDURE procedureName(parameter1, parameter2)
{
statements
}
PROCEDURE greetUser(name)
{
DISPLAY "Hello " + name
}
This procedure:
greetUsernameWhen you call a procedure, you tell the program to run it.
greetUser("Joe")
Output:
Hello Joe
A parameter is a variable used in a procedure definition that receives a value. Think of it like a placeholder.
PROCEDURE square(number)
{
DISPLAY number * number
}
Calling it:
square(5)
The value 5 is passed into the procedure.
| Term | Meaning |
|---|---|
| Parameter | Variable in the procedure definition |
| Argument | Actual value passed when calling |
Example:
square(5)
number5Some procedures return a value instead of just displaying something. Returning is useful when you want to store or reuse the result later.
PROCEDURE add(a, b)
{
RETURN a + b
}
Calling it:
result ← add(3, 4)
DISPLAY result
Output:
7
Modular design means breaking a large program into smaller, manageable pieces (modules). Each module should have one main job.
Ask for name
Print greeting
Ask for score
Calculate grade
Print grade
getName()
printGreeting()
calculateGrade()
printGrade()
Benefits:
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.
For the Create Performance Task, you must:
PROCEDURE calculateDiscount(price, isMember)
{
IF isMember = true
{
RETURN price * 0.8
}
ELSE
{
RETURN price
}
}
This includes: