Structure of a Function
The declaration syntax of a function is as follows:
return-value-type function-name( argument-list )
{
declarations and statements
}
The first line is the function header and the declaration and statement part is the body of the function.
return-value_type:
Function may or may not return a value. If a function returns a value, that must be of a valid data type. This can only be one data type that means if a function returns an int data type than it can only return int and not char or float. Return type may be int, float, char or any other valid data type. How can we return some value from a function? The keyword is return which is used to return some value from the function. It does two things, returns some value to the calling program and also exits from the function.
There may be some functions which do not return any value. For such functions, the return_value_type is void. ‘void’ is a keyword of ‘C’ language. The default return_value_type is of int data type i.e. if we do not mention any return_value_type with a function, it will return an int value.
Declarations and Statements:
This is the body of the function. It consists of declarations and statements. The task of the function is performed in the body of the function.
Example:
int square(int number)
{
int result = 0;
result = number * number;
return result;
}
Calling Mechanism:
How a program can use a function? It is very simple. The calling program just needs to write the function name and provide its arguments (without data types). It is important to note that while calling a function, we don’t write the return value data type or the data types of arguments.
//This
program calculates the square of a given number
#include
<iostream.>
using
namespace std;
int main()
{
int number, result;
result = 0;
number = 5;
// Getting the input from the user
cout <<
"Please enter the number to calculate the square";
cin >> number;
// Calling the function square(int number)
result = 'square(number)';
cout << "The square of"
<< number << "is" << result;
}
Declaration and Definition of a Function Declaration and definition are two different things. Declaration is the prototype of the function, that includes the return type, name and argument list to the function and definition is the actual function code. Declaration of a function is also known as signature of a function.
As we declare a variable like int x; before using it in our program, similarly we need to declare function before using it. Declaration and definition of a function can be combined together if we write the complete function before the calling functions. Then we don’t need to declare it explicitly. If we have written all of our functions in a different file and we call these functions from main( ) which is written in a different file. In this case, the main( ) will not be compiled unless it knows about the functions declaration.
Declaration:
int square
( int );
Definition:
int square
( int number)
{
return (number * number ) ;
}
Here is the complete code of the calling program.
//
Definition of the circleArea function.
double
circleArea ( double radius )
{
// the value of Pi = 3.1415926
return ( 3.1415926 * radius * radius ) ;
}
Sample Program 1
// This
program calculates the area of a ring
#include
<iostream>
using
namespace std;
// function
declaration.
double
circleArea ( double);
main ( )
{
double rad1 ;
double rad2 ;
double ringArea ;
cout << " Please enter the outer
radius value: ";
cin >> rad1 ;
cout << " Please enter the radius
of the inner circle: " ;
cin >> rad2 ;
ringArea = 'circleArea ( rad1 ) – circleArea
(rad2 )' ;
cout<< " Area of the ring having
inner raduis " << rad2 << " and the outer radius "
<<
rad1 << " is " <<
ringArea ;
}
double
circleArea ( double radius )
{
// the value of Pi = 3.1415926
return ( 3.1415926 * radius * radius ) ;
}
Tips
• We used functions for breaking complex problems into smaller pieces, which is a top-down structured approach.
• Each function should be a small module, self-contained. It should solve a well defined problem.
• Variable names and function names should be self- explanatory.
• Always comment the code.
0 Comments