Calling
Procedures
Learn how to call procedures, pass arguments, and use return values to write modular, reusable code in any programming language.
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.
Why Call Procedures?
Anatomy of a Procedure Call
Parameters vs Arguments
The Call-and-Return Model
Return vs No Return
RETURN(a + b)
sum ← add(3, 7) // sum = 10
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
Example 1: Simple Call
{
RETURN(x * 2)
}
a ← 7
result ← double(a)
DISPLAY(result)
Example 2: Multiple Parameters
{
area ← length * width
RETURN(area)
}
roomArea ← calculateArea(12, 8)
DISPLAY("Area: " + roomArea)
Example 3: Nested Calls
{
RETURN(n * n)
}
PROCEDURE sumOfSquares(a, b)
{
RETURN(square(a) + square(b))
}
result ← sumOfSquares(3, 4)
DISPLAY(result)
Example 4: No Return Value
{
REPEAT count TIMES
{
DISPLAY("★")
}
}
drawStars(5)
Execution Flow When Calling
Common Mistakes
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
Consider the following code:
PROCEDURE multiply(a, b)
{
RETURN(a * b)
}
result ← multiply(4, 6)
DISPLAY(result)
What value is displayed?
Question 2 of 5
In a procedure definition, the variables listed inside the parentheses are called:
Question 3 of 5
Consider the following code:
PROCEDURE mystery(x, y)
{
RETURN(x + y * 2)
}
DISPLAY(mystery(3, 5))
What value is displayed?
Question 4 of 5
Consider the following code:
PROCEDURE printMessage(msg)
{
DISPLAY(msg)
}
result ← printMessage("Hello")
What is the value of result after this code runs?
Question 5 of 5
Consider the following code:
PROCEDURE triple(n)
{
RETURN(n * 3)
}
PROCEDURE addOne(n)
{
RETURN(n + 1)
}
result ← addOne(triple(4))
What is the value of result?