C++ Notes: Function Parameters
Formal Parameters
Formal parameters are written in the function prototype
and function header of the definition. Formal parameters
are local variables which
are assigned values from the arguments when the function is called.
Actual Parameters or Arguments
When a function is called, the values (expressions)
that are passed in the call are called the arguments
or actual parameters (both terms mean the same thing).
At the time of the call each actual parameter is assigned to the
corresponding formal parameter in the function definition.
For value parameters (the default), the value of the
actual parameter is assigned to the formal parameter variable.
For reference parameters, the memory address of the
actual parameter is assigned to the formal parameter.
Value Parameters
By default, argument values
are simply copied to the formal parameter variables at the
time of the call. This type of parameter passing is called
pass-by-value. It is the only kind of parameter passing
in Java and C. C++ also has pass-by-reference (see below).
Reference Parameters
A reference parameter is indicated by following the formal parameter name
in the function prototype/header by an ampersand (&). The compiler will
then pass the memory address of the actual parameter, not the value.
A formal reference parameter may be used as a normal variable, without
explicit dereference - the compiler will generate the correct code for using
an address.
Reference parameters are useful in two cases:
- To change the value of actual parameter variables.
- To more efficiently pass large structures.
See Reference Parameters.
Additional topics
This summary doesn't cover some features: eg, variable parameter lists,
default parameter values, ....