Getting Started
Imagine you are writing instructions for a robot. If you repeatedly need the robot to pick up an object, you wouldn't want to write out the detailed steps—"extend arm, open gripper, move down, close gripper, retract arm"—every single time. Instead, you would create a single, named command like pickUpObject and simply use that name whenever needed. This is the core idea behind calling procedures: packaging reusable code to make programs simpler, more organized, and easier to manage.
What You Should Be able to Do
Identify the name and arguments in a procedure call.
Trace the flow of a program's execution as it enters and exits a procedure.
Determine the result of a code segment that contains one or more procedure calls.
Explain the relationship between the arguments in a procedure call and the parameters in the procedure's definition.
Use the value returned by a procedure in a larger expression or store it in a variable.
Key Concepts & Application
The Core Idea
A procedure is a named group of instructions that performs a specific task. A procedure call is the instruction that tells the program to pause its current execution, run the code inside that procedure, and then return to the next line in the main program.
Think of it like ordering a pizza. Your main task is "hosting a party." At some point, you need a pizza.
The Call: You call the pizza shop. The name of the shop, "Pizza Palace," is like the procedure's name.
The Arguments: You provide specific information, or arguments, with your order: "a large pizza" and "pepperoni."
Execution: The pizza shop (the procedure) now executes its own set of instructions: make dough, add sauce, add cheese, add pepperoni, bake. You don't need to know these details; you just trust the procedure to work.
The Return: Once finished, the shop "returns" a value to you: the finished pizza. You can then use this returned item in your main "party" program.
This process allows you to manage complexity. Instead of cluttering your main program with pizza-making details, you simply call a procedure that handles it for you.
Logic & Application
When a procedure is called, the values of the arguments are copied to the procedure's corresponding parameters. A parameter is a variable in the procedure definition that acts as a placeholder to receive an argument's value.
Annotated Pseudocode Examples
Example 1: A Procedure with No Return Value
This procedure simply performs an action (displaying a greeting) but does not send a value back.
// PROCEDURE DEFINITION
// 'personName' is the parameter that will receive the argument.
PROCEDURE displayGreeting (personName)
{
DISPLAY("Hello, " + personName + "!")
}
// MAIN PROGRAM LOGIC
// This is the procedure call.
// "Alice" is the argument being passed to the procedure.
displayGreeting("Alice")
// The program continues here after the procedure finishes.
DISPLAY("Welcome to the program.")
- Execution Flow: The program calls
displayGreetingwith the argument"Alice". The value"Alice"is copied into thepersonNameparameter. The procedure runs, displaying "Hello, Alice!". Control then returns to the main program, which displays "Welcome to the program."
Example 2: A Procedure That Returns a Value
This procedure calculates a value and uses the RETURN command to send it back to the calling code.
// PROCEDURE DEFINITION
// 'width' and 'length' are the parameters.
PROCEDURE calculateArea (width, length)
{
// Calculate the area
area <- width * length
// Send the calculated value back to the calling code
RETURN(area)
}
// MAIN PROGRAM LOGIC
// --- Method 1: Store the returned value in a variable ---
// The procedure is called with arguments 10 and 5.
// The returned value (50) is stored in the 'roomArea' variable.
roomArea <- calculateArea(10, 5)
DISPLAY("The area of the room is: ")
DISPLAY(roomArea)
// --- Method 2: Use the returned value directly in an expression ---
// The procedure call itself acts as the value it returns.
// The program calculates calculateArea(4, 3), gets 12 back,
// and then displays the full string.
DISPLAY("The area of the patio is: " + calculateArea(4, 3))
- Key Insight: A procedure call that returns a value can be treated just like any other value or variable in an expression.
Tracing & Analysis
To understand a program's behavior, you must trace its execution step-by-step, including jumps to and from procedures.
Logic Trace
Consider the following code that uses a procedure to double a number.
Pseudocode:
PROCEDURE doubleValue (num)
{
result <- num * 2
RETURN(result)
}
// Main Program
a <- 5
b <- doubleValue(a)
DISPLAY(b)
Execution Trace Table:
| Step | Location | Action | Variable Values |
|---|---|---|---|
| 1 | Main Program | a <- 5 | a is 5 |
| 2 | Main Program | Call doubleValue(a). The argument is the value of a, which is 5. | a is 5 |
| 3 | doubleValue | The parameter num receives the argument's value. | a is 5, num is 5 |
| 4 | doubleValue | result <- num * 2 | a is 5, num is 5, result is 10 |
| 5 | doubleValue | RETURN(result). The value 10 is sent back. | |
| 6 | Main Program | The returned value (10) is assigned to b. | a is 5, b is 10 |
| 7 | Main Program | DISPLAY(b) | Output to screen: 10 |
Key Terminology & Logic
A compact reference for the syntax and vocabulary related to calling procedures.
| Term / Syntax | Description |
|---|---|
PROCEDURE name(param1, ...) | Defines a named, reusable block of code with placeholders (parameters). |
name(arg1, ...) | A procedure call. It executes the procedure, passing arguments to it. |
RETURN (value) | Exits a procedure and sends a value back to the point of the call. |
| Argument | The actual data value sent to a procedure during a call. |
| Parameter | A variable in a procedure's definition that receives an argument's value. |
Core Concepts & Terminology
This section provides textbook definitions for key concepts and demonstrates core logic patterns.
Procedure Call: An instruction that causes a procedure to be executed. This interrupts the sequential flow of the program.
Argument: A specific value passed to a procedure when it is called. Arguments can be variables (e.g.,
myVariable) or literal values (e.g.,5,"hello").Parameter: A variable in the definition of a procedure that is used to receive an argument's value. It acts as a named placeholder within the procedure.
Return Value: The value that a procedure sends back to the code that called it. This allows procedures to be used for calculations where the result is needed elsewhere.
Abstraction: The process of simplifying complex reality by modeling classes appropriate to the problem. Procedures are a form of abstraction because they hide the complex details of a task behind a simple name.
Core Logic: Calling a Procedure
A procedure call transfers the flow of control and passes data.
// The values 12 and 15 are passed as arguments. // The procedure's return value is stored in 'total'. total <- calculateSum(12, 15)This single line hides the steps of addition, packaging them into a reusable command.
Core Logic: Using a Return Value Directly
The procedure call itself can be used as an expression that evaluates to the return value.
// The result of getAverage(90, 100) is used immediately // in the condition of the IF statement. IF (getAverage(90, 100) > 95) { DISPLAY("Excellent!") }
Core Skill Check
Apply your understanding with these short exercises.
Logic Tracing: What is the final value displayed after this pseudocode runs?
PROCEDURE modify(x) { RETURN(x + 5) } value <- 10 value <- modify(value) DISPLAY(value)Debugging: Identify the error in how the
calculateVolumeprocedure is being called in the main program.PROCEDURE calculateVolume(length, width, height) { ... } // Main Program boxVolume <- calculateVolume(10, 5)Application: Describe how a procedure named
drawCircle(x, y, radius)could be used to draw three different-sized circles on the screen without repeating code.
Common Misconceptions & Clarifications
Confusing Arguments and Parameters:
Misconception: "Arguments and parameters are the same thing."
Clarification: Arguments are the values you send into a procedure call (e.g.,
5,"hello"). Parameters are the variables that receive those values inside the procedure definition (e.g.,num,greeting). Think of it this way: the argument is the letter you mail; the parameter is the mailbox that receives it.
Forgetting that a Procedure Call is an Expression:
Misconception: "You can only store a procedure's result in a variable."
Clarification: For any procedure that returns a value, the call itself can be used anywhere a value of that type is allowed. The call
calculateArea(5, 4)literally evaluates to20and can be used directly, like inDISPLAY(calculateArea(5, 4) * 2).
Assuming a
RETURNis Always Required:Misconception: "Every procedure must end with a
RETURNstatement."Clarification: Many procedures exist only to perform an action, such as displaying text, moving a robot, or changing a color. These procedures do not need to return a value and will not have a
RETURNstatement.
Thinking Procedures Can Access All Variables:
Misconception: "A procedure automatically knows the values of variables in my main program."
Clarification: A procedure has its own scope. It only knows about the data passed into it via its parameters and any variables it creates internally. It cannot see or modify variables from the main program unless they are passed in as arguments.
Summary
Calling procedures is a fundamental technique for managing complexity and creating reusable code. The process involves a procedure call, which names the procedure to run and provides data through arguments. Inside the procedure, these arguments are received by parameters. The procedure executes its instructions and may send a return value back to the main program. This allows programmers to build large, sophisticated applications from small, understandable, and testable parts. This powerful technique is a primary example of abstraction, as it allows us to use a simple command to perform a complex task without needing to know the underlying details.