Functions save computer memory by allowing one group of statements to operate on multiple data items. However, their most significant contribution is allowing software developers to decompose or break down large, complex problems into smaller, more manageable conceptual units. Both aspects require that programs send or pass data into functions and return their results to the client who requested their service. Argument passing and functional return accomplish these two tasks.
Argument Passing Summary
Unlike Java, which only implements pass-by-value, C++ implements two distinct argument-passing mechanisms and synthesizes a third by coordinating a series of pointer operations. Consequently, C++ effectively has three distinct techniques for passing arguments into functions. One of the most critical distinctions between the different argument-passing techniques is the direction of the data flow. Pass-by-value only allows data to flow in one direction - into the function. Pass-by-pointer and pass-by-reference enable data to flow in both directions - into and out of the function. This section summarizes and illustrates the passing methods detailed in the previous sections.
Pass-By-Value (aka Pass-By-Copy)
Function Call
double t = triple(a);
Function Definition
double triple(double x)
{
return x * 3;
}
Pass-by-value or pass-by-copy.
Data Flow: in only (an IN mechanism)
Argument: an expression
Parameter: a local-scope variable
Notes:
The default argument passing mechanism
The function call passes a copy of an argument to to a function parameter; the argument is any valid expression
Any changes the function makes to the parameter(s) are to the local copy only (i.e., the function does not propagate the changes back to the argument(s) in the function call)
Pass-By-Pointer (aka Pass-by-Address)
Function Call
triple(&a);
Function Definition
void triple(double* x)
{
*x = *x * 3;
}
Pass-by-pointer or pass by address.
Data Flow: in and out (an INOUT mechanism)
Argument: the address of a variable
Non-Array Variables: the address is found with the address of operator: &
Array Variables: the address is the array name without brackets: [ and ]
Parameter: a local-scope pointer variable
The parameters must include the * as part of the variable definition
The variable name must be dereferenced, *x, whenever it is used in the body of the function
Notes:
Pass-by-pointer or address is just pass-by-value, but the value is the address of the argument variable
The function call passes the address of an argument variable to a function parameter. In the illustration, parameter x points to argument a
Changes made indirectly through the parameter, x, take place in the argument, a
Pass-By-Reference
Function Call
triple(a);
Function Definition
void triple(double& x)
{
x = x * 3;
}
Pass-by-reference.
Data Flow: in and out (an INOUT mechanism)
Argument: a variable
Parameter: a local-scope reference variable
Note that the & is overloaded to denote a reference variable and must appear as part of the local parameter definition
No other notation (operators or symbols) is needed when accessing or using the parameter
Notes:
Pass-by-reference is not supported by all programming languages and is implemented differently in C++ than in most other languages that do
The compiler maps the parameter to the same address as the argument
The function call and the function definition must be part of the same program
Changes made to the parameter, x, take place in the argument, a