Topic 3.12: Calling Procedures | AP CSP Big Idea 3 | APCSExamPrep.com

AP CSP Course Big Idea 3 3.12 Calling Procedures
3.12
Big Idea 3 • Algorithms & Programming

Calling Procedures

🕐 ~40 min FREE 📖 6 MCQ questions 🎮 Procedure Tracer game 💻 Live Python editor AAP-3.A

After this lesson, you will be able to:

  • Explain that calling a procedure runs its statements and then returns control to the caller
  • Distinguish an argument, the value passed in, from a parameter, the name that receives it
  • Match arguments to parameters by position and predict how swapping them changes the result
  • Use a returned value by storing or displaying it instead of discarding it
  • Tell apart a procedure that returns a value from one that only performs an action
  • Trace a program that calls procedures multiple times and combine their results
📈 Exam weight: Calling procedures appears throughout Big Idea 3 tracing and spot-the-bug items, and calling a student-developed procedure is directly rewarded on the Create Task. Swapped arguments and discarded return values are favorite traps.
💡 Think about this first

You use procedures you did not write every day. When you call a photo filter, you hand it an image and a strength, and it hands back an edited picture. You do not need to know how it works inside, only what to pass in and what comes back. That is calling a procedure: give it the right values in the right order, and use what it returns. This lesson is about getting both of those right.

What Calling a Procedure Does

A procedure (also called a function or a method) is a named group of programming instructions. Writing the procedure is one job; calling it is another. When you call a procedure, the program pauses where it is, jumps to the procedure, runs the statements inside it, and then returns control to the exact spot where the call happened. Execution continues from there. This topic is about using procedures that already exist. Writing your own is the next topic.

def greet(name):
    print("Hi " + name)

greet("Sam")
print("done")
PROCEDURE greet(name)
{
    DISPLAY("Hi " name)
}
greet("Sam")
DISPLAY("done")

Reading top to bottom: the def block only defines greet; nothing runs yet. The first line that actually executes is greet("Sam"), which jumps into the procedure, displays Hi Sam, and then hands control back so that print("done") runs next. Two lines are displayed, in order, because control always returns to the caller.

🎯 What the exam expects

The CED (AAP-3.A) says a procedure is "a named group of programming instructions." On the exam, Python uses def and AP pseudocode uses PROCEDURE name(parameters). Both mean the same thing. A call runs the instructions and then resumes after the call.

✍ Mini Exercise 1 • Predict the output
The procedure returns a value that is then printed. What does this display?
def f(a, b):
    return a - b
print(f(10, 4))

Arguments and Parameters: Matched by Order

When you call a procedure you may pass it values. Those values are the arguments. The names inside the procedure that receive them are the parameters. The connection between them is position: the first argument is copied into the first parameter, the second argument into the second parameter, and so on. The names do not have to match; only the order matters.

def power(base, exp):
    return base ** exp

x = power(2, 3)
print(x)
PROCEDURE power(base, exp)
{
    RETURN(base ^ exp)
}
x <- power(2, 3)
DISPLAY(x)

In power(2, 3) the argument 2 lands in the parameter base and 3 lands in exp, so the procedure returns 2 ** 3, which is 8. If you called power(3, 2) instead, you would get 3 ** 2, which is 9. Same procedure, different argument order, different result. This is a favorite exam trap.

⚠ Swapped arguments

Passing the right values in the wrong order is a silent bug: the code still runs, it just computes the wrong thing. Below, the procedure is fine but the call reverses the two arguments, so a subtraction meant to be 10 minus 3 becomes 3 minus 10.

# wanted 10 - 3, but the call is swapped
def minus(a, b):
    return a - b

print(minus(3, 10))   # prints -7, wrong
PROCEDURE minus(a, b)
{
    RETURN(a - b)
}
DISPLAY(minus(10, 3))

The procedure never changed. Only the call did. When you trace a call, always line up each argument with the parameter in the same position before you compute anything.

✍ Mini Exercise 2 • Spot the flaw
The procedure computes a - b. Why does this display -6?
def diff(a, b):
    return a - b
print(diff(4, 10))

Return a Value or Perform an Action

Procedures come in two flavors. Some return a value that the caller can store or use in a larger expression. Others just perform an action, such as displaying text, and hand back no useful value. Knowing which kind you are calling tells you whether you need to capture the result.

The critical habit: if a procedure returns a value, you must use that value by storing it in a variable or placing the call inside another expression such as print(...). A call like double(5) on a line by itself computes 10 and then throws it away, because nothing catches the return value. Nothing is displayed. Compare the two calls below.

def double(n):
    return n * 2

double(5)          # return value discarded
print(double(5))   # return value used, prints 10
PROCEDURE double(n)
{
    RETURN(n * 2)
}
double(5)
DISPLAY(double(5))

The first call, double(5), runs and returns 10, but the 10 is discarded because it is not stored or printed. The second call, print(double(5)), uses the return value, so 10 appears. A returned value is only useful if the caller does something with it.

🎯 Return vs display

Do not confuse return with print. return hands a value back to the caller; it does not put anything on the screen by itself. print (or DISPLAY) shows text but hands back no useful value. A procedure that only prints cannot have its result stored and reused.

✍ Mini Exercise 3 • Fill in the blank
The return value of one call becomes the argument to the next. Type exactly what this prints.
def twice(n):
    return n * 2
print(twice(twice(3)))
prints:

Tracing Programs That Call Procedures

Calls can happen many times, and each one runs the whole procedure again from the top. Use one disciplined routine and these traces become mechanical:

  1. Find the call. Match each argument to the parameter in the same position.
  2. Run the procedure's statements with those values. Note whether it returns a value or just acts.
  3. When it finishes, send any returned value back to the call, then continue right after the call.
  4. If a call is inside a loop, the procedure runs once per iteration, so count the iterations.
  5. Only a used return value affects the output. A discarded one changes nothing you can see.

For example, if add(a, b) returns a + b and you write a loop that calls t = add(t, k) for k in range(3) starting from t = 0, the procedure runs three times: t becomes 0, then 1, then 3. The final print(t) displays 3. Each call returned a value that the caller stored back into t, so the results accumulated.

Key Vocabulary

Term Definition Example
Procedure A named group of programming instructions (also called a function or method) def power(...)
Call Running a procedure's instructions, after which control returns to the caller power(2, 3)
Argument A value you pass into a procedure when you call it the 2 in power(2, 3)
Parameter The name inside the procedure that receives an argument by position base, exp
Return value The value a procedure hands back to the caller to store or use return base ** exp
Action procedure A procedure that performs a task, such as displaying text, and returns no useful value one that only calls print
📋 Create Performance Task • Calling a procedure and using its result

Procedural abstraction is central to the Create Task. Once you have defined a procedure, calling it and using its return value is how your program stays organized and handles varied input. The rubric rewards a program that calls a student-developed procedure and does something meaningful with what it returns.

A point-earning example

Imagine your program prices an order by calling a procedure that returns a total:

PROCEDURE calcTotal(price, qty) { RETURN(price * qty) } subtotal <- calcTotal(4, 3) DISPLAY(subtotal)

In your written response, name the call, explain the arguments, and describe how the returned value is used:

  • "The program calls calcTotal with the arguments 4 and 3, which are received by the parameters price and qty in that order."
  • "The procedure returns price * qty, and the program stores that returned value in subtotal so it can be displayed to the user."
  • "Calling the procedure instead of repeating its steps lets the program reuse one tested calculation, which manages complexity."

The trap to avoid

Two mistakes cost the point: passing arguments in the wrong order so the parameters receive the wrong values, and calling a procedure that returns a value without storing or displaying it, so the result is discarded. Line up arguments with parameters by position, and always capture a return value you need. See the full Create Task module →

📈
MCQ Practice
6 questions • Exam difficulty and above • Predict before you peek
Question 1 of 6Trace
Predict first: what value is stored in p, then what is added to it?
A procedure is defined and then called. What does this code segment display?
def f(x): return x + 1 p = f(3) print(p + f(p))
Incorrect. p is f(3), which is 4. Then f(p) is f(4), which is 5, and 4 + 5 is 9, not 7.
Incorrect. Recompute f(p): p is 4, so f(4) is 5. The sum is 4 + 5, which is 9.
Correct. f(3) returns 4, so p is 4. Then f(p) is f(4), which returns 5. The program prints 4 + 5, which is 9.
Incorrect. That would need f(p) to return 6, but f(4) returns 5, so the sum is 9.
Question 2 of 6Spot the bug
Predict first: which parameter actually receives the 2?
This call is meant to compute 8 divided by 2, which should display 4.0, but it displays 0.25. What is wrong?
def rate(k, n): return k / n print(rate(2, 8))
Correct. Arguments match parameters by position, so k gets 2 and n gets 8, giving 2 / 8 = 0.25. Calling rate(8, 2) would give the intended 4.0.
Incorrect. Using print would display the value but the wrong value would still be computed. The bug is the argument order.
Incorrect. Dividing integers is perfectly legal and returns a float. The bug is that the arguments are reversed.
Incorrect. Two parameters are exactly right. The problem is the order of the two arguments.
Question 3 of 6Spot the bug
The programmer expected this to display T5, but it only displays done. What is the reason?
def label(t): return "T" + str(t) label(5) print("done")
Correct. label(5) computes and returns "T5", but the call sits alone with nothing catching the value, so it is thrown away. To see it, write print(label(5)).
Incorrect. "T" + str(t) is valid string joining and produces "T5". The value is just never used.
Incorrect. label is called only once. The issue is that its return value is not used.
Incorrect. return only exits the procedure, not the program. done still prints; the returned value is simply discarded.
Question 4 of 6I, II, III
The procedure g is defined below. Which of the calls display 8?
def g(n): return n * 2
  • I. print(g(4))
  • II. print(g(8))
  • III. print(g(2) + 4)
Incorrect. I displays 8, but III also displays 8: g(2) returns 4, and 4 + 4 is 8.
Incorrect. II displays g(8), which is 16, not 8. II is not correct.
Correct. I is g(4) = 8. III is g(2) + 4 = 4 + 4 = 8. II is g(8) = 16, so only I and III display 8.
Incorrect. II is g(8), which returns 16, so II does not display 8.
Question 5 of 6NOT question
The procedure p returns x * x. Which call does NOT display 9?
def p(x): return x * x
Incorrect. p(3) returns 3 * 3, which is 9, so this DOES display 9.
Incorrect. p(-3) returns -3 * -3, which is 9, so this DOES display 9.
Incorrect. p(3) is 9 and 9 + 0 is 9, so this DOES display 9.
Correct. p(9) returns 9 * 9, which is 81, not 9. Every other call produces 9, so this is the one that does NOT.
Question 6 of 6Return vs action
Predict first: which of the two calls actually produces output?
A procedure returns a value. What does this code segment display?
def m(a, b): return a + b m(2, 3) print(m(4, 1))
Correct. The first call m(2, 3) returns 5 but nothing stores or prints it, so it is discarded. Only print(m(4, 1)) displays output, which is 5.
Incorrect. The first call's result is never printed because it is not inside print or stored, so 10 never appears.
Incorrect. Only one of the two calls is inside print. The first call's value is discarded, so 5 is displayed just once.
Incorrect. The second call is inside print, so it does display its return value, 5.
🎮 Lesson Game
Procedure Tracer
Trace each procedure call and predict exactly what it displays. 8 rounds.
0
Correct
1/8
Round
0
Streak 🔥
🐛 Python • what does the call display?
0/8
correct answers
💻 Live Python Code Editor
Practice Problems
Real Python runs right here in your browser. The first time you press Run, the Python engine loads (a few seconds); after that it is instant. Problems build from guided to Create-Task level. Use Hint if you are stuck, and check your output against the target.
Hints used: 0 • Solutions viewed: 0
Level 1 • Guided
Problem 1 of 8 • call and print the return
A procedure add(a, b) is defined for you. Call it with the arguments 6 and 9 and print the value it returns. Target output: 15
Level 2 • Argument order
Problem 2 of 8 • order matters
A procedure subtract(a, b) returns a - b. Arguments match parameters by position, so call it with 3 and 10 in the correct order to get 7, then print it. Target output: 7
Level 3 • Combine returns
Problem 3 of 8 • use two procedures
Two procedures are defined for you:
  • area(w, h) returns w * h
  • perimeter(w, h) returns 2 * (w + h)
Call both with w = 4 and h = 5 and print the area plus the perimeter. Target output: 38
Level 4 • Fix the bug
Problem 4 of 8 • unswap the arguments
This should print 7 (the result of 10 minus 3), but the call passes its arguments in the wrong order, so it prints -7. Fix the call. Target output: 7
Level 5 • Nested calls
Problem 5 of 8 • feed a return into a call
A procedure triple(n) returns n * 3. Use the value one call returns as the argument to another call, so you print the triple of the triple of 2. Target output: 18
Level 6 • Create Task style
Problem 6 of 8 • a procedure that calls a procedure
Two procedures model an order:
  • fee(n) returns n // 10, a ten percent fee in whole dollars
  • total(n) returns n + fee(n)
Call total for an order of 90 and print the result. Target output: 99
Level 7 • Challenge
Problem 7 of 8 • you write the calling logic
Open ended. Write the calling logic yourself. Two procedures are defined for you:
  • price(qty) returns qty * 4
  • discount(amount) returns amount // 10
A customer buys 3 apples and 5 bananas. Call price for each fruit and add the two results into a subtotal. Then subtract discount(subtotal) and print the final total on one line. Target output: 29
Level 8 • Challenge
Problem 8 of 8 • call in a loop and combine
Open ended. Write the calling logic yourself. A procedure score(correct, total) returns correct * 100 // total, a whole-number percent. The list results holds three (correct, total) pairs. Call score for each pair, add the three percents, then print the average percent (the sum divided by 3 using integer division). Target output: 73

Frequently Asked Questions

Calling a procedure runs the instructions inside it. The program jumps to the procedure, executes its statements, and then returns control to the exact place where the call was made, continuing from there.
An argument is a value you pass in when you call the procedure. A parameter is the name inside the procedure that receives that value. Arguments are matched to parameters by position: the first argument goes to the first parameter, and so on.
Because arguments are matched to parameters by order, not by name. If a procedure computes a - b and you call it with the values reversed, the wrong value lands in each parameter, so the subtraction runs in the wrong direction. The code still runs; it just computes the wrong answer.
return hands a value back to the caller so it can be stored or used in an expression, but it does not show anything on the screen. print (or DISPLAY) shows text but hands back no useful value. If a procedure returns a value, you must store or print it, or the value is discarded.
Calling a procedure lets your program reuse one tested calculation instead of repeating its steps, which manages complexity. In your written response you name the call, explain how the arguments fill the parameters, and describe how the returned value is used in the program.
📦
AP CSP Teacher SuperpackSlides, lesson plans, unit tests for all 5 Big Ideas, $249
Get the Superpack →
🏫
For teachers

The Superpack includes an editable Topic 3.12 slide deck with animated call-and-return traces, an argument-order and discarded-return bug bank, a print-ready procedure-calling worksheet, and a unit quiz. View what's included →

Get in Touch

Whether you're a student, parent, or teacher — I'd love to hear from you.

Just want free AP CS resources?

Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]