Functions save computer memory by allowing one group of statements to operate on multiple data items. However, their most significant contribution is enabling 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 to functions and return results to the client who requested their service. Argument passing and functional return accomplish these two tasks. This page summarizes the primary features of each passing mechanism; please see the preceding sections for more detailed explanations.
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 among argument-passing techniques is the direction of 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, pass-by-value, pass-by-copy, IN
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)
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
Data Flow Summary
function, data flow, IN, INOUT, pass-by-value, pass-by-pointer, pass-by-referenceDirection of data vs. passing technique.
C++ provides three argument-passing techniques: