DEFINING A FUNCTION
A function definition has a name, a parentheses pair
containing zero or more parameters and a body. For each parameter, there should
be a corresponding declaration that occurs before the body. Any parameter not
declared is taken to be an int
by default. It is good programming practice to declare all parameters.
Recently
compilers such as ANSIC may permit including the argument declaration within
the parentheses :
The general
format of the function definition is given below.
funtiontype funtionname (datatype
argumentl, datatype argument2...)
{
body of function
return something
}
(a) Declaring the type of a function
The function refers to the type of value it would return to the
calling portion of the program. Any of the basic data types such as int, float, char etc. may appear in the
function declaration. In many compilers, when a function is not supposed to
return any value, it may be declaration. In many compilers, when a function is
not supposed to return any value, it may be declared as type void, which informs the
compiler not to save any temporary space for a value to be sent back to the
calling program.
For example,
void function_name ( . . . )
int function_name ( . . . )
float function_name ( . . . )
char function_name ( . . . )
(b) Function name The function name can be any name conforming
to the syntax rules of the variables. Normally, a function name is made
relevant to the function operation, as it will be easy to keep a track of it,
whenever a transfer of similar function is used in the main program.
For example,
counter ( ) ;
square ( ) ;
display_value ( ) ;
output ( ) ;
(c) Formal arguments Any variable declared in the body of a
function is said to be local to that function. Other variables which are not
declared either as arguments or in the function body, are considered “global”
to the function and must be defined externally. The storage classes or scope of
variables are discussed subsequently in this chapter.
For example,
counter ( ) ;
square ( ) ;
display_value ( ) ;
output ( ) ;
(c) Formal arguments Any variable declared in the body of a
function is said to be local to that function. Other variables which are not
declared either as arguments or in the function body, are considered “global”
to the function and must be defined externally. The storage classes or scope of
variables are discussed subsequently in this chapter.
For example,
(1)
void
square (int a, int b)
/ / a, b are the formal
arguments
{
}
(2)
int counter ( float xl, float x2, int y1, int y2)
{ / / x1, x2, y1, and y2 are the
formal arguments
return (int value) ;
}
(3)
float output (void) / / function without formal
argument
{
statement;
return (float value) ;
}
(d) Function body After declaring the type of a
function, function name and formal arguments, a statement or a block of
statements is enclosed between the beginning and the end statements. In C++,
each function is declared almost like a main(
) function.
For example,
void all_add (int a, int c, float x, float y)
{
int i, j, n ; / / local variables , if any
}
PROGRAM
A program to demonstrate a function declaration, function
calling and function definition in C++.
#include
<iostream.h>
void main (void)
{
void display (void) ; / / function
declaration
display ( ) ; / / function definition
}
void display (void) / / function definition
}
cout << “ this is a test program
\n”;
cout << “ for demonstrating a
function call\n”;
cout << “ in C++ \n”;
}
Output
of the above program
this is a test program
for demonstrating a function call
in C++
The main function invokes the
display ( ) function. In C++, each function is almost like a separate program.
So the formal declaration such as type, being and end must be used in the
function also. The control will be transferred from the main function or from a
function to the called function. After it has executed all the statements in
the function the control switches back to the calling portion of the program.
No comments:
Post a Comment