6.3.4. Function Argument Passing Summary

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

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);
A function visualized as a machine with one input (a parameter) and one output (a return statement). The input may be any valid expression.
Function Definition
double triple(double x)
{
	return x * 3;
}
Pass-by-value or pass-by-copy.
  1. Data Flow: in only (an IN mechanism)
  2. Argument: an expression
  3. Parameter: a local-scope variable
  4. Notes:
    1. The default argument passing mechanism
    2. The function call passes a copy of an argument to to a function parameter; the argument is any valid expression
    3. 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);
A function visualized as a machine with one input (parameter). The function's return type is <kbd>void</kbd>, so it can't return a value with a <kbd>return</kbd> statement. The input (parameter) is a pointer, so the corresponding argument must be the address of a variable, and the function can return the data through the parameter/argument pair.
Function Definition
void triple(double* x)
{
	*x = *x * 3;
}
Pass-by-pointer or pass by address.
  1. Data Flow: in and out (an INOUT mechanism)
  2. Argument: the address of a variable
    1. Non-Array Variables: the address is found with the address of operator: &
    2. Array Variables: the address is the array name without brackets: [ and ]
  3. Parameter: a local-scope pointer variable
    1. The parameters must include the * as part of the variable definition
    2. The variable name must be dereferenced, *x, whenever it is used in the body of the function
  4. Notes:
    1. Pass-by-pointer or address is just pass-by-value, but the value is the address of the argument variable
    2. The function call passes the address of an argument variable to a function parameter. In the illustration, parameter x points to argument a
    3. Changes made indirectly through the parameter, x, take place in the argument, a

Pass-By-Reference

Function Call
triple(a);
A function visualized as a machine with one input (parameter). The function's return type is <kbd>void</kbd>, so it can't return a value with a <kbd>return</kbd> statement. The input (parameter) is a reference, so the corresponding argument must be a variable, and the function can return the data through the parameter/argument pair.
Function Definition
void triple(double& x)
{
	x = x * 3;
}
Pass-by-reference.
  1. Data Flow: in and out (an INOUT mechanism)
  2. Argument: a variable
  3. Parameter: a local-scope reference variable
    1. Note that the & is overloaded to denote a reference variable and must appear as part of the local parameter definition
    2. No other notation (operators or symbols) is needed when accessing or using the parameter
  4. Notes:
    1. Pass-by-reference is not supported by all programming languages and is implemented differently in C++ than in most other languages that do
    2. The compiler maps the parameter to the same address as the argument
    3. The function call and the function definition must be part of the same program
    4. Changes made to the parameter, x, take place in the argument, a

Data Flow Summary

A picture comparing the three passing techniques. Data can flow in only one way, into the function, with a pass-by-value call. Data can flow in either or both ways, in, out, or in and out, with pass-by-pointer and pass-by-reference calls.
Direction of data vs. passing technique. C++ provides three argument-passing techniques:
  1. Value: argument is IN only
  2. Pointer: argument is INOUT
  3. Reference: argument is INOUT