Default arguments

Default arguments

In this post we will see the concept of default argument in C++ with example. C++ allows 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. User specify default values when the user declare a function.

Consider a function declared as follows,
float area(int r, float Pi = 3.14);
The above prototype declares default value 3.14 to the argument Pi. A subsequent function call like –

A = area(7);  //One argument missing

This statement passes value 7 to r and lets the function use default value 3.14 for Pi.
The call A = area(7, 2.5) passes an explicit value 2.5 to Pi.

Only trailing arguments can have default values i.e. add defaults from right to left. A default value cannot provide to an argument in the middle of lists.

Advantages of default arguments

1. These are useful when in situations, when some arguments have some values.
2. It provides better flexibility to programmers by allowing to use particular arguments that are meaningful toparticular solution.
3. Use default arguments to add new parameters to the existing functions
4. Default arguments can be used to combine similar functions into a single function.

Let’s see an example to understand it more clearly.

Q. Write a program in C and C++ to explain default argument.

1. Program

C++ Code :

//Use of default arguments
#include<iostream>

using namespace std;

float area(int r, float Pi = 3.14);   //function prototype

int main()
{
    cout<<"\n Area is : "<< area(5);   //this uses default argument  //calling function with one argument
    cout<<"\n Area is : "<< area(5, 22/7);   //calling function with two arguments

    return 0;
}


float area(int r, float Pi)     //definition of function
{
    return Pi*r*r;
}

2. Output :

default argument in C++ with example

3. Explanation :

In this program we will calculate area of circle by making use of default argument. In this we first declare a function before start of the main function. While declaring this function we provide a default value to the variable Pi. Whenever the function call will have one value missing then the compiler will use default value of Pi. In main function we call the function twice with different values. In first call we pass only one argument i.e. 5, due to this it will use value of Pi as 3.14. In second call we are passing two arguments i.e. 5 and 22/7 (which gives 3) and thus it does not use default value of Pi but assigns Pi as 3. And in definition part we are returning area of circle as Pi * r * r.

4. Program Parts :

1. #include is a pre-processor directive. It is used to include header files.
2. iostream is header file which has certain commands that c++ supports. E.g. return, main, etc.
3. .h is an extension for header file.
4. float area is user defined function.
5. int is data type. It indicates that the program returns a value.
6. main() is main function that indicates the compiler that the user written programs starts from here.
7. cout is the keyword use to print a message.
8. cin is the keyword use to store values in variables.
9. return statement is use to return a value. If any error occurs the program will return 0.

Conclusion.

So this was the concept of default argument in C++ with example. We hope it was clear to you. If you find it useful then do share it with your programming buddies and friends. Also if you have any doubt regarding any post do tell us in the comment section.

For daily updates do follow us on Instagram. Visit again!

ErrorFreeProgram.

Leave a Reply

Your email address will not be published. Required fields are marked *