Default - Arguments in C++
-> C++ allows us to call a function without specifying all its Arguments.
-> In such a case, 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 functions is declared.
Here is an example:
float amount (float p, int t, float r=5.5);
value = amount (5000,7); // one Argument missing
value = amount (5000,5,7.25); // no missing Argument
-> we can not provide a default value to a particular Argument in the middle of an Argument list.
Ex: int mul (int i, int j=5, int k=10); // legal
int mul (int i=5, int j); // illegal
int mul (int i=0, int j); // illegal
int mul (int i=2, int j=5, int k=10); // legal
// C++ Program on default Arguments.
-> C++ allows us to call a function without specifying all its Arguments.
-> In such a case, 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 functions is declared.
Here is an example:
float amount (float p, int t, float r=5.5);
value = amount (5000,7); // one Argument missing
value = amount (5000,5,7.25); // no missing Argument
-> we can not provide a default value to a particular Argument in the middle of an Argument list.
Ex: int mul (int i, int j=5, int k=10); // legal
int mul (int i=5, int j); // illegal
int mul (int i=0, int j); // illegal
int mul (int i=2, int j=5, int k=10); // legal
// C++ Program on default Arguments.
#include<iostream>
using namespace std;
void interest (int p, float t, float r)
{
float SI;
SI= (p*t*r)/100;
cout<<SI;
}
int main()
{
interest(1000,1,12);
return 0;
}
using namespace std;
void interest (int p, float t, float r)
{
float SI;
SI= (p*t*r)/100;
cout<<SI;
}
int main()
{
interest(1000,1,12);
return 0;
}
OUTPUT:
120
120
*NOTE: // void interest(int p, float t, float r=24)
// interest(1000,1); (Rate is taken as 24 by default)
// interest(1000,1); (Rate is taken as 24 by default)