This text file contains information about parameter passing similar to the table-based PDF example at:
The method used to code parameter passing varies depending on the quantity of parameters and whether they are being passed into the function or returned from it. The generic code example below shows nine functions that pass parameters in different ways.
Normally, when we pass values into a function, those values are called "input parameters". However, when using the C++ programming language, if we want to return more than one value, we have to pass pointers (known as "output parameters") into the function to allow the return of each value using indirect assignment.
The comments within the functions describe data definition issues for each function. They use the single line comment notation which starts each comment with a double slash (//) and ends it with the cariage return that terminates the line of source code.
The program code does not perform any coherent purpose. It is intended only to show the possible methods for passing various quantities of parameters in and out of functions. The main function (at the end of the code at follows) declares two integers named AA and CC and two floats named BB and DD which are used as actual parameters in calling statements to the functions below.
#include <iostream>
using namespace std;
void Func1 ()
{
// Receives no input from parent and returns none either.
// Is called with a statement such as: Func1 ();
// Requires no body statements to pass parameters.
}
void Func2 (int A)
{
// Receives data in formal parameter A and returns none.
// Is called with a statement such as: Func2 (AA);
// Requires no body statements to pass parameters.
}
void Func3 (int A, float B)
{
// Receives data in formal parameters A and B and returns none.
// Is called with a statement such as: Func3 (AA, BB);
// Requires no body statements to pass parameters.
}
int Func4 ()
{
// Receives no input from parent, but returns an integer.
// Is called with a statement such as: CC = Func4 ();
// Requires a return statement to store a value in the function name.
// So the function name is treated as a value when it is called.
return 0;
}
int Func5 (int A)
{
// Receives data in formal parameter A and returns an integer.
// Is called with a statement such as: CC = Func5 (AA);
// Requires a return statement to store a value in the function name.
// So the function name is treated as a value when it is called.
return 0;
}
int Func6 (int A, float B)
{
// Receives data in formal parameters A and B and returns an integer.
// Is called with a statement such as: CC = Func6 (AA, BB);
// Requires a return statement to store a value in the function name.
// So the function name is treated as a value when it is called.
return 0;
}
void Func7 (int *C, float *D)
{
// Receives pointers ("output parameters") from the parent to be used to return data.
// Receives no "input parameters" from the parent.
// Is called with a statement such as: Func7 (&CC, &DD);
// Uses indirect assignment with pointers to return data, as in:
*C = 0; // Use pointer C to return an integer to an address in the parent function.
*D = 1.1; // Use pointer D to return a float to an address in the parent function.
}
void Func8 (int *C, float *D, int A)
{
// Receives pointers ("output parameters") from the parent to be used to return data.
// Receives data in formal "input parameter" A.
// Is called with a statement such as: Func8 (&CC, &DD, AA);
// Uses indirect assignment with pointers to return data, as in:
*C = 0; // Use pointer C to return an integer to an address in the parent function.
*D = 1.1; // Use pointer D to return a float to an address in the parent function.
}
void Func9 (int *C, float *D, int A, float B)
{
// Receives pointers ("output parameters") from the parent to be used to return data.
// Receives data in formal "input parameter" A and B.
// Is called with a statement such as: Func9 (&CC, &DD, AA, BB);
// Uses indirect assignment with pointers to return data, as in:
*C = 0; // Use pointer C to return an integer to an address in the parent function.
*D = 1.1; // Use pointer D to return a float to an address in the parent function.
}
int main ()
{
int AA=0, CC=0;
float BB=1.1, DD=1.1;
Func1 ();
Func2 (AA);
Func3 (AA, BB);
CC = Func4 ();
CC = Func5 (AA);
CC = Func6 (AA, BB);
Func7 (&CC, &DD);
Func8 (&CC, &DD, AA);
Func9 (&CC, &DD, AA, BB);
return 0;
}