6.3. Function Arguments and Parameters

Time: 00:01:57 | Download: Large, Large (CC), Small | Streaming, Streaming (CC) | Slides (PDF)

People, even experienced programmers, often use the terms "argument" and "parameter" interchangeably and rely on context to clarify the exact meaning (if an exact meaning is even needed). This practice generally doesn't cause any misunderstanding because whichever term they use describes the basic idea of passing data from a function call into a function for processing. Nevertheless, arguments are the values passed from a function call (i.e., they are the values appearing inside the call's parentheses) and sent to to a function. Parameters are local variables defined in the function (inside the parentheses in the function header) and used to hold the data passed into it.

The following example, based on pass-by-value, is the default passing technique and, therefore, the most familiar. However, C++ also supports two additional passing mechanisms: pass-by-pointer and pass-by-reference. The following sections explore and compare the details of the three passing techniques.

Arguments (aka Actual parameters) Parameters (aka Formal parameters) Effect
Appear in function calls Appear in function definitions
sqrt(x);
double sqrt(double y) { ... }
y = x;
double y = sin(M_PI / 4);
double sin(double angle) { ... }
angle = M_PI/4;
payment = payment(100000.00, 0.08, 30);
double payment(double p, double r, int n) { ... }
p = 100000.00;
r = 0.08;
n = 30;
Argument vs. parameter. The concepts of arguments and parameters are tightly linked. Like the sides of a coin, every argument has a corresponding parameter. The association is so strong we often use the two terms interchangeably. Sometimes, programmers use "formal" to denote the function side and "actual" to denote the call side: The program passes the actual parameters into the formal parameters . Except for pass-by-reference, passing data from an argument in the call to a parameter in the function behaves exactly like an assignment operation.
Argument and Parameter Names

When a function's arguments are variables, the argument names (in the function call) and the parameter names (in the function definition) may be the same, or they may be different.

  Same Different
Call (Arguments) function(x); function(y);
Definition (Parameters) void function(int x) { . . . } void function(int z) { . . . }

When a program passes data into a function, it matches the arguments (in the call) to the parameters (in the function) based on position. So, each argument must correspond to exactly one parameter based on position. The program determines position by counting the comma-separated elements from left to right. All three argument-passing techniques share this positional matching.

A figure showing a function call passing arguments to the parameters in a function definition. An arrow connects each argument with exactly one parameter. Arguments are matched to parameters by position: the first argument matches the first parameter, the second argument to the second parameter, and so on for all arguments and parameters.
Passing arguments to parameters. Programs match function arguments with corresponding parameters based on their position within each list. Matching occurs in English reading order, from left to right, and there must be one argument for each parameter. The arguments must be the same data type as the parameters or be a type that the compiler can automatically promote to the parameter type.