General Syntax

General Syntax

Subprograms

General Syntax

<Subprogram-Header>

<Body-of-Subprogram>

Example 1C++ 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 variablesor any other types of variables that are accessible to more than one subprogram to exchange values with the calling program.

Example 2In C++ we have the following:

int max,// to hold the number of values to read

average;// 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 referred to aslocal variables.

In most contemporary programming languages, local variables are by default stack dynamic.

In C/C++, a function’s local variable may be specifically declared to be static.

However, Ada subprograms, and the (class) methods of C++, Java, and C# have only stack dynamic local variables.

Example (C++ function with a static local variable)

The following function has a static local variable snum and a stack dynamic local variable dnum:

void funct( )

{

static int snum = 0;// static local variable

int dnum = 0// stack dynamic local variable

cout < endl < “snum=\t” < snum++ “\t dnum=\t” < dnum++;

}

Given the function main that follows:

int main( )

{

for( int ct = 0 ; ct < 3 ; ct++ )

funct( );

return 0;

}

Its execution will produce the following output:

Output

snum= 0dnum= 0

snum= 1dnum= 0

snum= 2dnum= 0

Using (Formal) Parameters in C++

In C++, there are two typesof (formal) parameters: value parameters, and reference parameters.

You distinguish a value parameter from a reference parameter by preceding a reference parameter with the ampersand symbol (&);

Example 3using value and reference parameters

  • In the example that follows,

vnum is a value parameter

rnum1 and rnum2 are reference parameters

void funct (int vnum , int & rnum1 , int & rnum2)

{

vnum += 7;

rnum1 += 4;

rnum2 = vnum + rnum1;

}

You use avalue parameterwhen you want to pass a value to the function when it is called, and

You use areference parameterwhen you want to pass a variable to the function when it is called.

When you call a function with parameters, you must specify between the parentheses that follow the function name the arguments (or actual parameters) as follows:

-A variable for each reference parameter, and

-Avalue (or an expression) for each value parameter

Each variable that you specify replaces its corresponding reference parameter in the body of the function.

Each value parameter behaves like a local variable that is initialized its corresponding value.

Example 3using 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= 5num2= 20num3 = 32

ExerciseWhich of the following call statements are invalid? Explain.

For each valid call statement also show the body of the function in the way it will be executed.

1)funct1(num1 +9, num1, num2);

2)funct1(2, num1);

3)funct1(num1, num2 + 5, num3);

4)funct1( 9, num1, num2 , num3);

Example 5One-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 6Two-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 prototype:float compute_pay (float income, float tax_rate, int exemptions = 1);

Function calls: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);orvoid swapper (int &, int &);

  1. int readvalue ( void );
  2. void funct1 (int vnum , int & rnum1 , int & rnum2);orvoid funct1 (int , int & , int &);
  3. void funct1 (int vnum , int * pt1 , int * pt2);orvoid funct1 (int , int * , int * );
  4. void funct3 (int list1[ ] , int list2[ ] , int num , int size); orvoid funct3 (int [ ] , int [ ] , int , int );
  5. void funct4 (int list[ ] [5] , int num , int size);orvoid funct4 (int [ ] [5] , int , int );

Java and C# do not have function prototypes.

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-valueis an implementation of the in-mode parameters. The value of the argument is used to initialize the corresponding formal parameter.

Pass-by-resultis 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-resultis an implementation of in-out mode parameters. It is a combination of pass-by-value and pass-by-result.

Pass-by-referenceis another implementation of in-out mode parameters.

Overloading Subprograms

Very often, two or more functions of a program conceptually perform the same task, but the number and/or the data types of some of their arguments are different.

For example, you may have a function to compute the average of three integer values, and another one to compute the average of three double precision floating-point values.

You may also have a function to compute the average of two integer values, and another one to compute the average of three integer values.

Giving the same name to these functions can make the program easier to read and understand

In C++ two or more functions may have the same name, as long as there is a way to distinguish them based on their parameters.

This feature is called function overloading.

The compiler determines the right version of the function to call from a set of overloaded functions by inspecting the arguments specified in the function call.

The example in Figure 14 illustrates the use of overloaded function names in a source module.

Function Name Overloading

Line Number

1/****************************************************************

2Program to compute the average of two integer values, the average

3of three integer values and the average of two double precision

4 floating-point values.

5***************************************************************/

6#include <iostream>

7using namespace std;

8int ComputeAverage( int, int);

9 // to compute the average of two integer values

10int ComputeAverage( int, int, int);

11//to compute the average of three integer values

12double ComputeAverage(double, double);

13 //to compute the average of two floating-point values

14

15int main()

16{

17int result1,// the average of two integer values

18result2;// the average of three integer values

19double result3;//the average of two floating-point values

20

21result1 = ComputeAverage(4, 5);// call to function defined in line 35

22

23result2 = ComputeAverage(5, 4, 6);// call to function defined in line 40

24

25result3 = ComputeAverage(4.0, 5.0);// call to function defined in line 45

26

27cout < “\n\nresult1=\t” < result1 < “\n\nresult2=\t”

31< result2< “\n\nresult3=\t” < result3;

32return 0;

33}

34

35int ComputeAverage(int value1, int value2)

36{

37return (value1 + value2) / 2;

38}

39

40int ComputeAverage(int value1, int value2, int value3)

41{

42return (value1 + value2 + value3) / 3;

43}

44

45double ComputeAverage(double value1, double value2)

46{

47return (value1 + value2) / 2;

48}

OUTPUT

result1=4

result2=5

result3=4.5

Generic Subprograms

Function Templates

A function template is an alternative way to overloading a function name when all the functions have the same number of parameters but with different data types.

For example, suppose that in a program we have to write three functions: The first interchanges the values of two character variables, the second one interchanges the values of two integer variables, and the last one interchanges the values of two double precision variables. Instead of writing the three separate functions, we can write a template function that can be used in any of the above three situations.

C++ automatically generates separate function template specializations to handle each type of call appropriately.

A template function definition begins with the keyword template followed by a template parameter list specified in angle brackets as follows:

template < class T1, class T2, . . . ,class Tn >or

template < typename T1, typename T2, . . . ,typename Tn >

Where:T1, T2, . . . , Tn are the type parameters that represent the different data types that will be used in the definition of the function template.

Example

Function template of functions to interchange the values of two variables

template <class T>

void swapValues( T & var1 , T & var2)

{

T temp;

temp = var1;

var1 = var2;

var2 = temp;

}

Function template to find the maximum value in a list.

template < class T >

T findMax ( T list[ ], int size )

{

T maxvalue;// to hold the maximum value

for (maxvalue = list[0], int i = 1; i < size; i ++)

if (maxvalue < list[i])

maxvalue = list[i];

return ( maxvalue );

}

Example

Function template that receives variables in two data types and output the maximum size of those variables/ data types.

Note:

C++ provides a compile-time unary operator named sizeofwith the following syntax:

sizeof <variable>returns the number of bytes used to represent <variable>.

sizeof( <variable>)returns the number of bytes used to represent <variable>.

sizeof <array-namereturns the number of bytes in the array with name array-name.

sizeof( <array-name>)returns the number of bytes in the array with name array-name.

sizeof ( <data-type>)returns the number of bytes used to represent a variable with the data type <data-type>.

Examples

int inum, list[ 10 ];

double dnum;

cout < “\n size of inum is:\t” < sizeof inum;

cout < “\n size of int is:\t” < sizeof (int);

cout < “\n size of list is:\t” < sizeof (list);

cout < “\n size of dnum is:\t” < sizeof dnum;

cout < “\n size of double is:\t” < sizeof (double);

Output

size of inum is:4

size of int is:4

size of list is:40

size of dnum is:8

size of double is:8

Note that since the operator sizeof is a compiler-time operator, it cannot be used in a function to determine the size of an array passed to that function as an argument as follows:

void funct( int list[ ] )

{

int size = sizeof ( list )/ sizeof( int );

. . .

}

template <class T1, class T2>

int largest( T1 &var1, T2 &var2 )

{

int max;

if ( sizeof( var1 ) > sizeof(var2) )

max = sizeof(var1);

else

max = sizeof(var2);

return max;

}

Notes

  • Functions templates do not have function prototypes: they must therefore be included in any source module in which they are called.
  • One way to use functions templates in a program is to place them in a header file and to include that header file in any program where the function template is called.

Example

/*-- Program to read two values and to compute the difference of the largest value minus the smallest -*/

template <class T>

void swapValues( T & var1 , T & var2)

{

T temp;

temp = var1;

var1 = var2;

var2 = temp;

}

int main( )

{

int inum1, inum2;

double dnum1, dnum2;

/*------read two integer values and compute the difference of the largest minus the smallest -----*/

cin > inum1 > inum2;

if(inum1 < inum2)

swapValues( inum1, inum2);

cout < inum1 – inum2;

/*- read two double precision values and compute the difference of the largest minus the smallest */

cin > dnum1 > dnum2;

if(dnum1 < dnum2)

swapValues( dnum1, dnum2);

cout < dnum1 – dnum2;

return 0;

}

Exercise

Write the function template of functions to find the maximum of three values.

Other Issues

Nested subprograms.

Implementing Subprograms with Stack-Dynamic Local Variables

Activation Record

Local variables
Parameters
Dynamic link:
(Pointer to the base of the activation record instance of the caller )
Return address

Top of the stack

Exercises Chapter 81c, 2, 3c, 4, 5, 6, 8, 9 pages 383-385.

©2011 Gilbert NdjatouPage 1