Chap-3 Functions in C++: Function
Chap-3 Functions in C++: Function
FUNCTION
A function is a subprogram that can performed specific task on data and return a value.
Every C++ program has at least one function, main ().
A C++ program contains a special function called main (). The main () function is an entry point
of program execution; it contains the code that tells the computer what to do as a program
execution.
In C language, it is not specify any return type for the main () function, but in C++ the main ()
returns a value of type int to the operating system, so in C++ main () function is written as:
int main()
{
. . . . .
. . . . .
return 0;
}
But the return type of main () is a default int type, so do not need to write a keyword int in the
main ().
FUNCTION PROTOTYPE
In above example, the function name is max, return type is int and number of arguments
(parameters) is three and all the parameters types are int.
We can also declare a function with an empty argument list, as in the following example:
void display ( ) ;
In C++, this means that the function does not pass any parameters. It is identical to the statement
void display ( void ) ;
In function declaration, the names of the arguments are dummy variables and therefore they are
optional, but in the function definition names are required because the arguments must be
referenced inside the function.
Example: float volume (int a, float b, float c) ;
{
float v = a * b * c ;
...
...
}
The function volume ( ) can be invoked (called) in a program as follows:
float cube1 = volume (x , y, z) ;
In function calling, the variables are called actual parameters. Remember, the calling statement
should not include type names in the argument list.
CALL BY REFERENCE
In call by value, the called function creates a new variable of the same type as the argument and
copies the argument value in it. So its take more space in memory or duplication of variable
occurs, which will waste the memory space.
But in call by reference, address is passed of original variable instead of value passed to the
function.
The primary advantage of call by reference is that function can access the actual variable in the
calling program.
Below example shows how can we pass the address of original variables using call by reference.
#include <iostream.h>
#include <conio.h>
void main ()
{
int x , y ;
getch () ;
}
temp = a ;
a = b ;
b = temp ;
cout << After interchang, the values are:<< a << b;
}
Output:
Enter the value of x:- 10
Enter the value of y:- 20
After interchang, the values are:
20
10
RETURN BY REFERENCE
#include <iostream.h>
#include <conio.h>
void main ()
{
int x , y , z ;
Here return type of max ( ) is int &, the function returns reference to a or b(not value), then
function call such as max(x,y) will a reference to either x or y depending on their values.
INLINE FUNCTIONS
One of the advantages of using functions in a program is to save some memory space, when
function is likely to be called many times. However, every time a function is called, it takes a lot
of extra time in executing a series of instructions for tasks such as jumping to the function.
C++ has a solution of this problem. To eliminate the cost of calls to small functions, C++
proposes a new feature called inline function.
An inline function is a function that is extended in line when it is invoked. The compile replaces
the function call with the corresponding function code.
The inline functions are defined as follows:
inline return-type function-name (argument list)
{
Function body ;
}
Example (Inline Function)
#include<iostream.h>
#include <conio.h>
Reduces the execution time, an inline function is about 10000+ times faster than a normal function.
Default arguments are specified when the function is declared. The compiler looks at the
prototype to see how many arguments a function uses and alerts the program for possible default
values.
For example,
The value of variable c is default assign to 10 in function declaration, so no need to pass this
value into the function at the time of calling function. For example,
amount( x, y ) ;
In a function calling no need to pass three values, because third value is default argument in
function declaration.
Default parameters are must be assign left to right direction. Below example shows how to
define default argument in function.
Invalid:
Valid:
int AreaCube (int length, int width = 25, int height = 1);
#include<iostream.h>
#include<conio.h>
void max(int,int i=20);
main()
{
clrscr();
int a;
Max is:= 20
In C++, an argument to a function can be declared as const keyword, which are shown below:
int square (const int a) ;
float area (const float r) ;
int strlen (const char * p) ;
int length (const string & s) ;
#include<iostream.h>
#include<conio.h>
void square(const int s=6);
main()
{
clrscr();
square();
getch();
return 0;
}
void square(const int x)
{
int y = x * x ;
cout<<"Square is= "<<y;
}
Output:
Square is:= 36
FUNCTION OVERLOADING
Overloading refers to the use of the same thing for different purposes. C++ also permits
overloading of functions. This means that we can use the same function name to create functions
that perform a variety of different tasks. This is known as function polymorphism in OOP.
Using the concept of function overloading, we can design a family of functions with one function
name but with different argument list.
All the functions perform different operations depending on matching of argument list in the
function calls.
For example, a sum ( ) function is overloaded in different types of data, the declaration are:
int sum ( int x, int y ) ;
int sum ( int x, int y, float z ) ;
flaot sum ( float x, float y ) ;
double sum ( double x ) ;
#include<iostream.h>
#include<conio.h>
int sum(int,int);
int sum(int,int,int);
float sum(int,float);
double sum(float,float);
main()
{
clrscr();
cout<< sum(10,10)<< endl;
cout<< sum(10,20,30)<< endl;
cout<< sum(10,4.25f)<<endl;
cout<< sum(4.25f,6.25f);
getch();
return 0;
}