Unit 3: Programming · Procedures

Article

Parameters

6 min read

The greet procedure always says the same thing. A real program wants to greet Mia, then Omar, then anyone else. You do not want to write a new procedure for every name. You want one procedure that you can give a name to.

Parameters and arguments

A parameter is a variable written inside the brackets of a procedure's definition. It is an empty slot that waits for a value. An argument is the actual value you put in the brackets when you call the procedure. When the call happens, the argument is copied into the parameter.

AP pseudocode
PROCEDURE greet(name)
{
    DISPLAY("Hello")
    DISPLAY(name)
}
greet("Mia")
greet("Omar")
Runs in your browser

The output is Hello, Mia, Hello, Omar. Inside greet, the parameter name holds "Mia" during the first call and "Omar" during the second.

WordWhere it isIn this example
Parameterin the definitionname
Argumentin the call"Mia" and "Omar"

A way to remember: parameter and procedure both start with p, and the parameter belongs to the procedure's definition. Because one procedure now works for many values, we say the parameter generalises the procedure.

Check your understanding

Which values are the arguments in this program?

AP pseudocode
PROCEDURE area(width, height)
{
    DISPLAY(width * height)
}
area(3, 4)

Arguments match parameters by position

A procedure can have more than one parameter. Separate them with commas. The first argument goes into the first parameter, the second into the second, and so on. Only the order matters, not the names of any variables you pass in.

AP pseudocode
PROCEDURE subtract(a, b)
{
    DISPLAY(a - b)
}
subtract(10, 3)
subtract(3, 10)
Runs in your browser

The first call sets a to 10 and b to 3, so it displays 7. The second call sets a to 3 and b to 10, so it displays -7. The same two numbers in a different order give a different answer.

You must give exactly one argument for each parameter. Calling subtract(10) or subtract(1, 2, 3) is an error.

Check your understanding

What is displayed as a result of running this program?

AP pseudocode
PROCEDURE subtract(a, b)
{
    DISPLAY(a - b)
}
subtract(2, 9)

Arguments are worked out first, then copied

An argument does not have to be a plain value. It can be a variable or a whole expression. The computer works out its value first, and then copies it into the parameter.

AP pseudocode
PROCEDURE addTen(n)
{
    n ← n + 10
    DISPLAY(n)
}
x ← 5
addTen(x)
DISPLAY(x)
Runs in your browser

The output is 15 and then 5. The call copies the value 5 into n. The procedure then changes n to 15, but x is a separate variable and stays 5. A parameter is a new variable that lives inside the procedure. Changing it does not change the variable you passed in.

Worked example

AP pseudocode
PROCEDURE showSum(a, b)
{
    DISPLAY(a + b)
}
showSum(2, 3)
x ← 4
showSum(x, x * 2)

Trace each call. Work out the arguments, copy them into the parameters, then run the body.

CallArguments worked outabShown
showSum(2, 3)2 and 3235
showSum(x, x * 2)4 and 4 * 2 = 84812

The program shows 5 and then 12.

Common mistakes

  • Mixing up the two words. The parameter is in the definition. The argument is in the call.
  • Getting the order wrong. subtract(3, 10) is not the same as subtract(10, 3).
  • Thinking the procedure changes the variable you passed in. It only gets a copy of the value.
  • Using a parameter name outside the procedure. The name name or n exists only while the procedure runs.
  • Giving too few or too many arguments. The number of arguments must equal the number of parameters.

On the exam

Two kinds of question appear again and again. One asks which value a parameter holds in a particular call. The other asks what a program displays when it calls a procedure with variables or expressions as arguments. For both, write a small table: the call, the parameter values, and the result.

Key words

Parameter: a variable in a procedure's definition that receives a value when the procedure is called.

Argument: the value given to a parameter when the procedure is called.

Generalise: make one procedure work for many different values by using a parameter.

AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse, this site. All lessons and questions here are written by JrCodex.