Topic 3.12: Calling Procedures | AP CSP Big Idea 3 | APCSExamPrep.com
Calling Procedures
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
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.
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.
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.
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.
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.
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.
def twice(n): return n * 2 print(twice(twice(3)))
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:
- Find the call. Match each argument to the parameter in the same position.
- Run the procedure's statements with those values. Note whether it returns a value or just acts.
- When it finishes, send any returned value back to the call, then continue right after the call.
- If a call is inside a loop, the procedure runs once per iteration, so count the iterations.
- 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
|
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:
In your written response, name the call, explain the arguments, and describe how the returned value is used:
- "The program calls
calcTotalwith the arguments4and3, which are received by the parameterspriceandqtyin that order." - "The procedure returns
price * qty, and the program stores that returned value insubtotalso 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 →
Get a free AP CSP question every day
Join 3,000+ students. Daily practice, study tips, and exam strategies.
4.0, but it displays 0.25. What is wrong?T5, but it only displays done. What is the reason?g is defined below. Which of the calls display 8?-
I.
print(g(4)) -
II.
print(g(8)) -
III.
print(g(2) + 4)
p returns x * x. Which call does NOT display 9?add(a, b) is defined for you. Call it with the arguments 6 and 9 and print the value it returns. Target output: 15
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
-
area(w, h)returnsw * h -
perimeter(w, h)returns2 * (w + h)
w = 4 and h = 5 and print the area plus the perimeter. Target output: 38
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
-
fee(n)returnsn // 10, a ten percent fee in whole dollars -
total(n)returnsn + fee(n)
total for an order of 90 and print the result. Target output: 99
-
price(qty)returnsqty * 4 -
discount(amount)returnsamount // 10
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
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
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.🔗 Continue studying
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.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]