-->

Default arguments with example.

1 minute read

  Default arguments with example. 

C++ allows us to call a function without specifying all its arguments. In such cases, the function assigns a default value to the parameter which does not have a matching argument in the function call.
Default values 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.
A default argument is checked for type at the time of declaration and evaluated at the time of call.
Default arguments are useful in situation where some arguments always have the same value.

EXAMPLE
#include<iostream.h>
int add(int a, int b, int c=5)
int main()
{
    int a=5,b=5,ans;
    ans=add(a,b);
    cout<<”ans=”<<ans;
    return 0;
}
int add(int a, int b, int c)
{
    int ans;
    ans=a+b+c;
    return ans;
}