FUNCTION

You can write a program as a collection of functions - using functions allows you to write  modular programs. That is you can break down large problems into a collection of small problems and then write a function to solve each of the smaller problems. This programming approach avoids duplication of code and lets you write programs that are easier to read, write, debug, and maintain. To create a user-defined function, you need to perform the following steps:

FUNCTION DECLARATION

You must declare all functions in your program before you can use them.

Syntax               return_type   func_name(arg_type(s));

You declare the functions before the main() function.

Example Explanation
int test( );
void main()
Declare a function named test. This function returns an integer value does not have any passing arguments.
float calculateArea(float, int);
void main()
Declare a function named calculateArea. This function has two passing arguments. The first argument is float data type and the second one is integer data type. The function also return a float value.
void hello( );
void main()
Declare a function named hello. This function does not have any passing argument as well as any return value.

 

FUNCTION DEFINITION

A function definition is a named block of code. A block of code is defined by opening and closing curly braces ({ and }).

Syntax:                  return_type   function_name(argument list)
{

                                function body;

                            }

You define a function after a main() function. If you defnine a function before a main(), then you don't need to declare a function.

Example Example Example
double  findArea(double radius)
{double area;
area= radius*radius*3.1416;
return area;
}
void print_value(double radius)
{ cout<<"The circle area is "     <<radius*radius*3.1416;
}
double calculateCost(int items, double unitprice)
{ return  items* unitprice;
}

                                                       

FUNCTION CALL

When C++ encounters a function name in C++ program, it calls, or invokes, the function, causing program execution to stop in the current  or calling function. Program control is then transferred to the function that is being called, and the computer executes the statements in the called function.

When the function finish its execution, control returns to the point in the calling function at which the call it was made. If a function returns a value other than void, then the calling program should use the return value.

The following examples will demonstrate the creation of a user-define function.

Example Example
//Declare the function hello
void hello( );
void main()
{ hello( ); // Call the function hello
}
//Define a function hello
void hello( )
{ cout<<"Welcome to C++ programming"<<endl;
}

 

//Declare a function
double findArea(double);
void main( )
{double r, CirclArea;
cout<<"Enter radius"<<endl;
cin>>r;
CircleArea=findArea( r ); //Call the function
cout<<"The area is "<<CircleArea;
}
//Define the function
double  findArea(double radius)
{double area;
area= radius*radius*3.1416;
return area;
}

 

OVERLOADING FUNCTION

You can overload by giving the same name to more than one function. The function must have different numbers of arguments or the arguments must be of a different data type. C++ will figure out what function to call based on its arguments. Overloading functions allows C++ programmers to choose the most meaningful name for a function. It also permits the use of polymorphic code (applying to object-oriented programming).

Example
double average(double,double);
double average(double, double, double);
double average(int,double);
void main()
{  double x, y, z;
x=average(3.5 , 4.7);
y=average(10 , 20.4);
z=average(12.4, 4.8, 5.5);
cout<<x<<' '<<y<<' '<<z;
}
double average(double x, double y)
{ return (x+y)/2;
}
double average(int x, double y)
{ return (x+y)/2;
}
double average(double x, double y,double z)
{ return (x+y+z)/3;
}
Output
4.1  15.2   7.56

DEFAULT ARGUMENT

In C++, you can assign default values to function arguments. When actual arguments are not provided in the call to a function, C++  will use these default values. The default values for the arguments appear in the function declaration. You must follow one rule when creating default values: not all arguments need default values, but once a default value has been specified for an argument, all of the subsequent argument must have a default value as well.

 

Example
double average(double val1= 1.0, double val2= 3.0, double val3=5.0);
void main()
{  double x, y, z;
x=average( );
y=average(3.5 , 4.7);
z=average(12.4, 4.8, 5.5);
cout<<x<<' '<<y<<' '<<z;
}
double average(double x=1.0, double y=3.0,double z=5.0)
{ return (x+y+z)/3;
}
Output
3.0  4.4  7.6

 

STANDARD LIBRARY FUNCTION

A library is a collection of C++ built-in functions that you can use in many programs. To use the library functions, you need to know the function's name, its purpose, the number of arguments it expects and the data types of the arguments, its purpose, the number of  arguments it expects and the data type of the arguments, the value returned by the function and its data type, and the header file that you should #include to ensure that your program will have access to the correct function declaration for the function.

The pow( ) function, for example, is a C++ standard library function that you can use to raise a number to power of two, three, and so on. The example below shows the function declaration for this function and a C++ program that uses the pow( ) function.

double   pow(double, double)

The function declaration for pow( 0 is found in <math.h>. The function expects two arguments, both of which are doubles. The function returns a double, which is the value of the first argument raised to the power of the second argument.

Example

#include<iostream>
#include<math>
using namespace std;

void main()
double x = 3.0;
double y=5.0;
double result;
result=pow(x, y);
cout << result<<end;
}

Output

243