Search This Blog

Wednesday, 24 October 2012

Funtion with Default Arguments

#include<iostream.h>
int sum(int x=10,int y=20,int z=30);
void main()
{
cout<<"sum()="<<sum();
cout<<"sum(15)"<<sum(15)<<endl;
cout<<"sum(15,25)="<<sum(15,25)<<endl;
cout<<"sum(15,25,35)="<<sum(15,25,35)<<endl;
}
int sum(int x,int y,int z)
{
return(x+y+z);
}

Output:
sum()=60
sum(15)=65
sum(15,25)=70
sum(15,25,35)=75

Function with Default Arguments:
One of the most useful facilities available in C++ is the facility to define default argument values for functions. The default values are given in the function prototype declaration. Whenever a call is made to a function without specifying an argument, the program will automatically assign values to the parameter from the default function prototype declaration.
 

No comments:

Post a Comment