Function with Default Argument


Default argument is the argument that is assigned value at the time of declaring the function. It is not compulsory to pass value for the default argument at the time of calling the function, it will automatically assign default value that is specified at the time of declaring the function.
However if you specify value for the default argument explicitly at the time of calling the function it will override the default value.
Default arguments are widely used in the situation where value of some arguments always remains same.
Default arguments are always specified from right to left in the function prototype.

     

Examples:
float area (float radius, float pi=3.14); // Valid
float area (float pi=3.14, float radius); // Not Valid


Example:
# include <iostream.h>
int main ()
{
float radius;
float area (float r, float pi=3.14);
cout<<"Enter Radius: ";
cin>>radius;
cout<<"Area of Circle is: "<<area(radius);
return 0;
}
float area (float r, float pi=3.14)
{
return (pi*r*r);
}
Output:
Enter Radius: 3
Area of Circle is 28.26

Download Projects


Download Programs