Skip to main content
← Unit 3: Algorithms and Programming

Code Scholars Tutoring · AP Computer Science Principles

3.3 Procedures, Parameters, and Return Values

How it works

Procedures name a reusable block of logic; parameters let the same procedure work with different values.

Connect the argument in the call to the parameter inside the procedure, then trace local changes.

Common error

Do not assume a procedure displays a value if it only returns one, or returns a value if it only displays one.

Worked example

Distinguish returning a value from displaying it.

Data / pseudocode

PROCEDURE addFee(price, fee) {
  RETURN(price + fee)
}
result <- addFee(12, 3)
DISPLAY(result * 2)

The arguments 12 and 3 become the parameter values. The procedure returns 15 to the assignment but does not display it. The caller then displays 30. Reusable parameters let the same logic work for different prices and fees.

Exercises

3.3.1

If addFee(8, 2) is called without DISPLAY, does the procedure shown print 10? Explain.

Show solution

No. It returns 10 to its caller; returning and displaying are distinct operations. A surrounding DISPLAY would be needed to show it.

3.3.2

Write a procedure countAbove(values, cutoff) that returns the number of list items strictly greater than cutoff.

Show solution

Solution pseudocode

PROCEDURE countAbove(values, cutoff) {
  count <- 0
  FOR EACH value IN values {
    IF (value > cutoff) { count <- count + 1 }
  }
  RETURN(count)
}
For [2, 7, 7] and cutoff 2, the returned value is 2.

Need help with this topic?

Work through questions with a tutor and get help with the steps you find difficult.

Get help with this topic