Functions in C++ - Advanced features

The method and concepts discussed in the first tutorial on functions (C tutorials) hold equally true for C++ as well. However C++ functions supports some additional features as well. This tutorial deals with advance C++ function features.

Call by reference : Apart from the method discussed in the first tutorial (highlighing the use of pointer arguements) ,  C++ provides the '&'(referential) operator for calling by reference.

 

Syntax

 
function prototype

return-type function-name(data-type & arguement_name);
function definition

return-type function-name(data-type & arguement_name)

{

inside the body , the aguement is to be used as any other variable(not as pointer variable)

}
function call

function_name(arguement name);

//the variable is passed like any other variable of its data type and as an address 

Example :: SWAP program revisited

#include<iostream>
using namespace std;

void swap_ref(int &a,int &b);
void swap_val(int a,int b);

 int main()
{
int a=3,b=6;
printf(“\na=%d  b=%d”,a,b);
swap_val(a,b);

printf(“\na=%d  b=%d”,a,b);
swap_ref(a,b);  
printf(“\n a=%d  b=%d”,a,b);

return 1;
}

void swap_ref(int &a, int &b)
{ //function acceptsa reference
a=a+b; //to original parameter variable
b=a-b;
a=a-b;
}
void swap_val(int a, int b)
{
a=a+b;
b=a-b;
a=a-b;
}

OUTPUT:
a=3  b=6
a=3  b=6
a=6  b=3
 

Default arguments

C++ provides the option of  providing default values to the arguments being passed to a function.

Consider the example

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

long int sum(int n,int diff=1,int first_term=1 )
{
long  sum=0;;
for(int i=0;i<n;i++)
{
cout<<setw(5)<<first_term+diff*i;
    sum+=first_term+diff*i;
}
return sum;
}

int main()
{
cout<<endl<<"Sum="<<setw(7)<<sum(10)<<endl;
//first term=1; diff=1,n=10
//sums the series 1,2,3,4,5………10

cout<<endl<<"Sum="<<setw(7)<<sum(6,3,2)<<endl;
//first term=1; diff=2,n=10
//sums the series 2,5,8,11,14,17

cout<<endl<<"Sum="<<setw(7)<<sum(10,2)<<endl;
//first term=1; diff=2,n=10
//sums the series 1,3,5………..19

return 1;
}
 

IMPORTANT

all the parameters with default values should lie to the right in the signature list i.e. the default arguments should be the trailing arguments—those at the end of the list.

 

when a function with default arguments is called, the first argument  in the call statement is assigned to the first argument in the definition, the 2nd to 2nd and so on.

This becomes more clear from the last call to sum() in the above example where value 10 is assigned to n and 2 is assigned to diff and not first_term.

 

 the default argument values appear in the prototype as well as definition.

You still may omit variable names in the prototypes.

The syntax then being 

int xyz(int =2,char=5);

 

Function overloading

c++  permits the use of two function with the same name. However such functions essentially have different argument list. The difference can be in terms of number or type of arguments or both.

 

This process of using two or more functions with the same name but differing in the signature is called function overloading.

But overloading of functions with different return types are not allowed.

In overloaded functions , the function call determines which function definition will be executed. 

 

The biggest advantage of overloading is that it helps us to perform same operations on different datatypes without  having the need to use separate names for each version.

 

Example

#include<iostream>

using namespace std;

int abslt(int );
long abslt(long );
float abslt(float );
double abslt(double );

int main()
{

int intgr=-5;
long lng=34225;
float flt=-5.56;
double dbl=-45.6768;
cout<<" absoulte value of "<<intgr<<" = "<<abslt(intgr)<<endl;
cout<<" absoulte value of "<<lng<<" = "<<abslt(lng)<<endl;
cout<<" absoulte value of "<<flt<<" = "<<abslt(flt)<<endl;
cout<<" absoulte value of "<<dbl<<" = "<<abslt(dbl)<<endl;
}
int abslt(int num)
{
if(num>=0)
return num;
else
 return (-num);
}
long abslt(long num)
{
if(num>=0)
return num;
else return (-num);
}
float abslt(float  num)
{
if(num>=0)
return num;
else return (-num);
}
double abslt(double  num)
{
if(num>=0)
return num;
else return (-num);
}

OUTPUT
 absoulte value of -5 = 5
 absoulte value of 34225 = 34225
 absoulte value of -5.56 = 5.56
 absoulte value of -45.6768 = 45.6768

 The above function finds the absolute value of any number int, long, float ,double. In  C, the above is implemented as a set of different function abs()-for int, fabs()-for double ,labs()-for long.

The use of overloading may not have reduced the code complexity /size but has definitely made it easier to understand and avoided the necessity of remembering different names for each version function which perform identically the same task.

>>Kindly post your doubts and suggestions on our discussion forum.