Default Arguments in C++

Default Arguments in C++

3 mins read3.4K Views Comment
clickHere
Updated on Jan 16, 2023 18:04 IST

A default argument is a value in the function declaration automatically assigned by the compiler if the calling function does not pass any value to that argument.This article is explaining the working of the Default Arguments in C++ with programming example.

2022_11_MicrosoftTeams-image-7.jpg

In this article, we will discuss default arguments in C++. To understand this, you must have a clear understanding of C++ functions. 

When we define a function, we pass specific values to it, called arguments. By default, function arguments in C++ are passed “by value.” They are also known as parameters. 

Now, let us understand the concept of default arguments in detail. We will be covering the following sections:

What are Default Arguments?

A C++ default argument is a value in the C++ function declaration automatically assigned by the compiler if the calling function does not pass any value to that argument.

However, if arguments are passed when the function is called, the default arguments are ignored. 

Rules for defining default arguments –

  • The values passed in the default arguments can be overwritten if a value is explicitly passed to the function. 
  • During the function call, the values are copied starting from left to right.
Encapsulation in C++ with a Real-Life Example
Encapsulation in C++ with a Real-Life Example
Encapsulation is a process of combining member functions and data members in a single unit called a class. The purpose is to prevent access to the data directly. In this...read more
Function Overloading in C++
Function Overloading in C++
Do you want to know about function overloading? This article will clear the concepts of function overloading with programming examples.
OOPs Concepts in C++
OOPs Concepts in C++
OOP concepts in C++ involve creating objects from classes, bundling data and methods. Inheritance forms new classes from existing ones, while polymorphism enables interchangeable usage of objects, enhancing code modularity...read more

Preparing for a C++ interview? Check out the Top C++ Interview Questions and Answers

Also Read – Understanding Operators in C++

Learn more: Basics of C Programming Language

Related Read – Binary Search in C++

Working of Default Arguments

Let’s look at the following cases of a code snippet:

Case 1:

2022_11_image-21.jpg

Here, when the function temp() is called, both the default arguments are used by the function. 

Case 2:

2022_11_image-23.jpg

Here, when the function temp(4) is called, the first argument becomes 6, while the second argument still uses the default value. 

Case 3:

2022_11_image-24.jpg

Here, when the function temp(4, -3.5) is called, both the default arguments are overridden by the values explicitly passed to the function inside main().

Case 4:

2022_11_image-25.jpg

In this case, when the function temp(3.5) is called, the function throws an error because we are trying a pass a floating point number, which should be the second argument, and it cannot be passed without passing the first argument. 

Example of Default Arguments in C++

Example: 

 
#include <iostream>
using namespace std;
//define the default arguments
void show(char = '$', int = 5);
int main() {
    
    int count = 7;
    cout << "No argument is passed: ";
    //Default arguments will be displayed
    show(); 
    
    cout << "First argument is passed: ";
     //only the second argument will be default
    show('#'); 
    
    cout << "Both arguments are passed: ";
    show('*', count); 
    return 0;
}
void show(char c, int n) {
    for(int i = 1; i <= n; ++i)
    {
        cout << c;
    }
    cout << endl;
}
Copy code

Output:

2022_11_image-26.jpg

What have we done here?

We first create a function called show() that has no arguments. In this case, the function show() uses the default arguments c = ‘$’ and n = 5.

Next, the function show(‘#’) is called with one argument. So, the first default argument is overwritten with the value c = ‘#,’ and the second default argument is retained, i.e., n = 5.

Now, the function show(‘*,’ count) is called with both arguments. So, both the default arguments are overwritten with the values c = ‘*’ and n = 7. In this case, the default arguments are not used at all.

Points to Remember about Default Arguments

  • The default argument must be implicitly convertible to the parameter type.
  • Once a default value for an argument is given, all subsequent arguments must also have default values. 
  • If the default arguments are defined in the function definition instead of the function prototype, the function must then be defined before the function call.

Endnotes

I hope this article was helpful for you in understanding default arguments in C++. If you want to learn more about C++ and solidify your basics, you can explore our articles on C++.

Download this article as PDF to read offline

Download as PDF
clickHere
About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio

Comments

We use cookies to improve your experience. By continuing to browse the site, you agree to our Privacy Policy and Cookie Policy.