AP® Computer Science Principles · Big Idea 3 · Topic 3.12

Procedure Calls, Parameters, and Arguments

Calling a procedure hands it input values, called arguments, which land inside the procedure as its own named variables, called parameters. The procedure runs using those values and can hand a result back.

Think of a procedure as a worker you can hand a job to. You give it what it needs, it does the work, and it gives you back an answer. The things you hand over at the call site are arguments. Inside the procedure, each argument lands in a parameter, a variable that only exists for that one run of the procedure. The procedure's code never knows or cares where the arguments came from. It just uses the parameters as if they were ordinary variables.

The connection between argument and parameter is pure position and value, not name. When you call a procedure with two arguments, the first argument's value is copied into the first parameter, the second into the second, and so on. The parameter is a fresh box with its own label, filled with a copy of whatever value was sitting in the argument. That is why a parameter named amount works perfectly well when the caller passes in a variable named price, or a literal number, or the result of another expression. Only the value travels across; the name stays behind.

This is the hinge between writing procedures and actually using them. Once you can call a procedure correctly, matching arguments to parameters in order, you can reuse the same block of logic with endless different inputs, which is the whole point of abstraction. It also sets up why a procedure needs exactly as many arguments as it has parameters: each parameter needs a value to work with, and a missing argument leaves a parameter with nothing to hold, which causes an error rather than letting the procedure guess.

Key terms

Procedure
A named, reusable block of code that performs a task when called.
Argument
A value supplied at the call site when a procedure is invoked.
Parameter
A variable inside a procedure's definition that receives an argument's value.
Call site
The specific place in code where a procedure is invoked with its arguments.

Watch it explained

  1. How it worksHow does an argument written at the call site become a parameter inside the procedure?
  2. Worked exampleTrace a call to a procedure taking two numbers and displaying their product, for 6 and 7.
  3. Common mistakeWhy isn't the name of a parameter tied to the name of the variable passed into it?
  4. Going furtherWhat happens when a procedure is called with fewer arguments than it has parameters?

Check yourself

Your turn1 of 2
A procedure takes two numbers and displays their product. It is called with the arguments 21 and 20. What value does it display?

End of topic.