Beruflich Dokumente
Kultur Dokumente
General Syntax
<Subprogram-Header> <Body-of-Subprogram> Example 1 C++ subprograms:
void swapper (int &num1, int &num2) { int temp; temp = num1; num1 = num2; num2 = temp; } int computeAvge ( void ) { int total = 0, value; for (int j = 0 ; j < 10 ; j++) { cin >> value; total += value; } return (total /10); } There are two types of subprograms: procedures and functions.
A procedure corresponds to a void function in C/C++. A function corresponds to a C/C++ function that returns a value. A subprogram may either have (formal) parameters or not. A subprogram that does not have parameters uses global variables to exchange values with the calling program. In C++ we have the following: // to hold the number of values to read // to hold their average
void computeAvge ( void ) // read values and compute their average { int total = 0, value; for (int j = 0 ; j < max ; j++) { cin >> value; total += value; } average = total / max; }
int main( ) { /*-------------------- read ten values and compute their average -----------------------------*/ max = 10; computeAvge ( ); cout << endl << the average of those values is:\t << average; return 0; }
Local Variables
Variables defined in the body of a subprogram are called local variables. In most contemporary programming languages, local variables are by default stack dynamic. In C/C++, a functions local variable may be specifically declared to be static.
However, Ada subprograms, and the methods of C++, Java, and C# have only stack dynamic local variables.
In C++, there are two types of (formal) parameters: value parameters, and reference parameters. A value parameter is used by a function to receive a value from the calling function. A reference parameter is used by a function to supply and/or receive a value from the calling function. When a function is called, the arguments (or actual parameters) must be specified as follows:
-
For a value parameter, you must specify a value (or an expression): A memory location is created for each value parameter, and it is initialized with that value. For a reference parameter, you must specify a variable name: This variable name replaces the reference parameter in the body of the function. The data types of the arguments and the parameters must be compatible.
Example 3
void funct1 (int vnum , int & rnum1 , int & rnum2) { vnum += 7; rnum1 += 4; rnum2 = vnum + rnum1; }
int main( ) { int num1 = 5, num2 = 16 , num3; funct1 (num1 , num2 , num3); cout << endl << num1= << num1 << \tnum2= << num2 << \tnum3= << num3; return 0; } The output of this program is: num1= 5 num2= 20 num3 = 32
Exercise
1) 2) 3) 4)
funct1(num1 +9, num1, num2); funct1(2, num1); funct1(num1, num2 + 5, num3); funct1( 9, num1, num2 , num3);
Example 4
void funct1 (int vnum , int * pt1 , int * pt2) { vnum += 7; *pt1 += 4; *pt2 = vnum + *pt1; } int main( ) { int num1 = 5, num2 = 16 , num3; funct1 (num1 , &num2 , &num3); cout << endl << num1= << num1 << \tnum2= << num2 << \tnum3= << num3; return 0; } The output of this program is: num1= 5 num2= 20 num3 = 32
Example 5
void funct3 (int list1[ ] , int list2[ ] , int num , int size) { for (int j = 0; j < size, j++) list2[ j ] = list1[ j ] + num; }
int main( ) { int vala [ 5 ] = { 3, 9, 4, 6, 8}, valb[ 5 ]; funct3 (vala , valb , 7, 5); return 0; } Example 6 Two-dimensional arrays as parameters
void funct4 (int list[ ] [5] , int num , int size) { for (int r = 0; r < size, r++) for (int c = 0 ; c < 5 ; c++) list[ r ][ c ] = list[ r ][ c ] + num; } int main( ) { int table [6][ 5 ] = { {3, 9, 4, 6, 8}, {2, 3, 4, 5, 6} , {3, 2, 5, 6, 3} , { 9, 6, 5, 2, 8} , {2, 4, 3, 6, 7} , { 4, 6, 5, 7, 8}}; funct4 (table , 10 , 6); return 0; }
Example
Function header: Function calls: float compute_pay (float income, float tax_rate, int exemptions = 1) pay = compute_pay (20000.0, 0.15); pay = compute_pay (25000.0, 0.18, 4); Note: In C++, parameters with default arguments must appear last in the argument list.
5. void funct3 (int list1[ ] , int list2[ ] , int num , int size); or void funct3 (int [ ] , int [ ] , int , int );
Receive a value from the corresponding argument (actual parameter). Semantic model: in mode. Example: value parameter in C++. Transmit data to the corresponding argument. Semantic model: out mode. Or to receive and transmit data to the corresponding argument. Semantic model: In-Out mode. Example: reference parameter in C++.