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; |
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.