Posted By:
3 Jul, 2013 12:54 pm PDT

Functions in C++

What are functions? How to implement functions?
Attachment:None


       In C++, two functions can have the same name if their parameter type or number is different. The property of the function to have same name is known as overloading. Hence functions can be overloaded if they either have a different parameter type or different number of parameters.

For example

#include<iostream.h>

int volume(int l,int b,int h)

{

int vol;                                                               ‘volume’ function with 3 parameters

vol=l*b*h;

return vol;

}

 

 

int volume(int b,int h)

{

int vol;                                                                       ‘volume’ function with 2 parameters

vol=0.3*b*h;

return vol;

}

void main()

{

int v1,v2;

v1=volume(2,3,4);

v2=volume(2,4);

cout<<”\nVolume of rectangle:”<<v1;

cout<<”\nVolume of cone:”<<v2;

}

Was it useful?  




Please login to reply to this problem.