NIOS Computer Science: Chapter 12 – Function Part 5

Download PDF of This Page (Size: 395K)

Image of function prototyping

Image of Function Prototyping

Image of function prototyping

I. Function Prototype

The function prototype informs the compiler about the functions to be used in a program, the argument they take and the type of value they return.

Functions which do not return any value are known as void functions.

II. Arguments to a Function

Sometimes the calling function supplies some values to the called function. These are known as parameters.

The variables which supply the values to a calling function called actual parameters.

The variable which receive the value from called statement are termed formal parameters

Consider the following example that evaluates the area of a circle.

# include < iostream.h >

void area (float);

void main ( )

{

float radius;

cin >> radius;

area (radius);

}

void area (float r)

{

cout << “The area of the circle is” << << “\n”;

}The statement area (radius), that invokes the function, may be written as

area (6.0);

This will evaluate and display the area of a circle with radius 6.0 Here radius is called actual parameter and r is called formal parameter.

III. Return Type of a Function

In the above program, area of a circle display in the function area ( ) itself.

If the area of a circle required in the calling function main ( ), the function area ( ) may return the area evaluated to function main ( ).

This is possible through the return statement. The program may be written as:

#include<iostream.h>

float area(float);

void main( )

{

float radius, y;

cin >> radius ;

y = area (radius )

cout < < “The area of the circle is ” < < y;

}

float area (float r)

{

return ;

}

In function prototype, void is replaced by float, this indicates that value returned by the function area to main function is float.

In the calling function variable y is assigned the value returned by function area ( ). Thus, variable y will be assigned the area of a circle.