Sie sind auf Seite 1von 6

Subprograms

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

Example 2 int max, 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.

Using (Formal) Parameters in C++


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

using value and reference parameters

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)

Which of the following call statements are invalid? Explain.

funct1(num1 +9, num1, num2); funct1(2, num1); funct1(num1, num2 + 5, num3); funct1( 9, num1, num2 , num3);

Example 4

using value and pointer parameters

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

One-dimensional arrays as parameters

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; }

Specifying Default Arguments


In C++, FORTRAN 95, Ada, and PHP (but not in Java), formal parameters may have default values: A default value is used if no actual parameter (argument) is passed for that formal parameter.

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.

Subprogram Declarations: Function Prototypes


Example 7 1. void swapper (int &num1, int &num2); 2. int readvalue ( void ); 3. void funct1 (int vnum , int & rnum1 , int & rnum2); 4. void funct1 (int vnum , int * pt1 , int * pt2); 6. void funct4 (int list[ ] [5] , int num , int size); Java and C# do not have function prototypes. or void funct1 (int , int & , int &); or void funct1 (int , int * , int * ); or void funct4 (int [ ] [5] , int , int ); or void swapper (int &, int &);

5. void funct3 (int list1[ ] , int list2[ ] , int num , int size); or void funct3 (int [ ] , int [ ] , int , int );

Parameter Passing Methods


Formal parameters are used to do one of the following:
-

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++.

Implementation Models of Parameter Passing


Pass-by-value is an implementation of the in-mode parameters. The value of the argument is used to initialize the corresponding formal parameter. Pass-by-result is an implementation of the out-mode parameters. The formal parameter acts as a local variable. But just before the control returns to the calling program, its value is copied to the corresponding argument (which must be a variable). Pass-by-value-result is an implementation of in-out mode parameters. It is a combination of pass-by-value and pass-by-result. Pass-by-reference is another implementation of in-out mode parameters.

Overloading Subprograms Generic Subprograms

Das könnte Ihnen auch gefallen