Unit#5 Functions in C++    Root Finding
Course Outline Return to Unit
File Structure in the Presence of User Defined Functions:


When we have additional functions besides the main( ), we can arrange our file in various ways. In this course we suggest that you use the following arrangement:

#include<………>

…………………

Prototype of function Afun

Prototype of function Bfun

……………………..
void main( )
{
…….statements ( call to Afun,
                           call to Bfun, etc.
}

// definition of first function
float Afun( … parameters,…)
{
….. statements …….
}

//  definition of second function
double Bfun( … parameters,…)
{
….. statements …….
}

  ……………………………

Types of user defined functions
The types of the functions can be any of the types used for variables, such as int , float, double, etc.. The type refers to the value, which will be returned via the name of the function. If the name of the function is not used to return a value, then the type of the function is designated as void.

Parts of a user defined function:
There are three different parts of each function:

a) the prototype ( placed up on top before the main( ) function )
b) the call to the function ( somewhere in the main( ) or another function )
c) the definition of the function ( an independent piece of code, which communicates with the main    or with another function via the parameters of the function and its name )

Calling a user defined function
Information from the calling function is passed to the user defined function during the call to the function and via its arguments. Some examples of statements calling a user defined function are given below:

footage = trapez( 3.4, 4.5, 1.98 ); Function trapez has three parameters: the length of the small base len1, the length of the long side len2, and the height h.  The arguments 3.4, 4.5, and 1.98 are given to the parameters of the function.  The value of area is calculated by the function trapez and it is returned to the variable footage of the calling program.
autotrapez( 3.4, 4.5, 1.98); Here the same problem is handled in a different way. The function does not return a value to the calling function, but it calculates and also outputs the value of the area directly to the screen.
superautotrapez;
Here we have a function, which does not receive information from the calling function and it does not return any value either. It is designed to prompt the user for the values of len1, len2 and h and then calculate the area and output it to the screen.

Values returned to the calling function:
There are three different categories of functions from the point of view of the number of values returned to the calling program.

a) functions with a single return: These functions are very much like the library functions we have used. Their definition is something like

float rectangle( float base, float height )
{
float area;
area = base * height;
return ( area);
}

The parameters base and height ( both of them declared as float) are used to calculate the area. The return statement ensures that the calculated area is returned to the calling program via the name of the function rectangle. Same exact idea as when we use cos( 3.156) to calculate the cosine of 3.156 radians.

The calling statement of this function may look like this:

z = rectangle( 5.6, 8.91);

or

cout <<" area is: " << rectangle( 3.2, 7.8);

The first line of the definition (header) when terminated by ; serves as the prototype of the function. In this case it is:

float rectangle( float base, float height );

b) functions with no return: These functions do not return any value to the calling function. Their definition is something like :

void rectangle( float base, float height )
{
float area;
area = base * height;
cout <<"\n the area is: " << area;
}

The parameters base and height ( both of them declared as float) are used to calculate the area, which is outputted to the screen from within the function. Here we don't need a return statement.

The calling statement of this function may look like this:

rectangle( 5.6, 8.91);

The first line of the definition (header) when terminated by ; serves as the prototype of the function. In this case it is:

void rectangle( float base, float height );

c) functions with several returns: These functions must return more than one value to the calling function. They use the idea of passing parameters by name ( rather than by value only). Their definition is something like :

void rectangle( float base, float height , float& area, float& perimeter)
{
area = base * height;
perimeter = 2 * base + 2 * height;
}

The parameters base and height ( both of them declared as float) are used to calculate the area as well as the perimeter, which are passed to the calling program via the parameters area and perimeter ( note the & after their type). Here again we don't need a return statement.

The calling statement of this function may look like this:

rectangle( 5.6, 8.91, m_square, meters);

The value of the area will be transferred to the memory location m_square and the value of the perimeter will be transferred to the memory location meters.

The first line of the definition (header) when terminated by ; serves as the prototype of the function. In this case it is:

void rectangle( float base, float height , float& area, float& perimeter);

Note again the & before the parameters area and perimeter.

Go back to anchor page of unit5 and check the full examples given.

 

Jacob Y. Kazakia © 2001 All rights reserved