×

Default arguments in C++

Arguments in a function are defined as the values supplied when the function is called. The source is the values supplied, and the destination is the receiving function. Let us now examine the idea of default arguments in further depth.

What is a default Argument?

The default argument is the value in the job declaration that automatically provides if the caller's function does not add value to that argument.

Default argument characteristics

The rules for specifying default parameters are as follows:

  • The default parameters do not have constant values. If the value is supplied to the function, these values can be overwritten. Otherwise, the previously specified value is retained.
  • The values are transferred from left to right when the function is called.
  • All of the values that will be assigned a default value will be displayed on the right.

Example:

#include < iostream >  
#include < bits/stdc++.h >
#include <stdlib >
using namespace std ;  
int sum ( int a , int b , int c = 0 , int d = 0 ) // Here there are two values in the default arguments   
{ // Both c and d are initialised to zero   
    return ( a + b + c + d ) ; // return sum of all parameter values  
}  
int main ( )  
{  
    cout << sum ( 10 , 15 ) << endl ; // a = 10 , b = 15 , c = 0 , d = 0  
    cout << sum ( 10 , 15 , 25 ) << endl ; // a = 10 , b = 15 , c = 25 , d = 0  
    cout << sum ( 10 , 15 , 25 , 30 ) << endl ; // a = 10 , b = 15 , c = 25 , d = 30  
    return 0 ;  
}

OUTPUT:

25
50
80
….
Process executed in 0.11 seconds
Press any key to continue.

Explanation

  • When this function is invoked, it gets to the sum definition. There, it sets a to 10 and b to 15, and the rest of the values are set to zero by default because no value is supplied. And the aggregate of all the values yields the result 25.
  • When this function is invoked, a remains 10, b remains 15, and the third parameter, c, is set to 25 rather than zero. And the final result is still 0. The total of a, b, c, and d equals 50, and this is returned as output.
  • There are four argument values supplied into the function in this function call, with a as 10, b as 15, c as 25, and d as 30. The total of all the values produces the result 80.

Let us look at another example:

#include < iostream >  
#include < bits/tsdc++.h >
#include < stdlib >
using namespace std ;  
int sum ( int a , int b , int c = 0 , int d = 0 ) // Here there are two values in the default arguments   
{ // Both z and w are initialized to zero   
    return ( a + b + c + d ) ; // return sum of all parameter values  
}  
int sum ( int a , int b , float c = 0 , float d = 0 ) // Here sum is overloaded with two float parameter values   
{ // This results in ambiguity   
    return ( a + b + c + d ) ;  
}  
int main ( )  
{  
    cout << sum ( 10 , 15 ) << endl ; // a = 10 , b = 15 , c = 0 , d = 0  
    cout << sum ( 10 , 15 , 25 ) << endl ; // a = 10 , b = 15 , c = 25 , d = 0  
    cout << sum ( 10 , 15 , 25 , 30 ) << endl ; // a = 10 , b = 15 , c = 25 , d = 30  
    return 0 ;  
}  

OUTPUT:

error: call of overloaded ' sum ( int , int ) ' is ambiguous
  cout <<  sum ( 10 , 15 ) << endl ; // a = 10 , b = 15 , c = 0 , d = 0
                    ^
note: candidate: int sum ( int , int , int , int )
 int sum ( int a , int b , int c = 0 , int d = 0 ) // Here there are two values in the default arguments 
 note: candidate: int sum ( int , int , float , float )
 int sum ( int a , int b , float c = 0 , float d = 0 ) // Here sum is overloaded with two float parameter values 

Explanation:

If we call a sum function with all the arguments ( a, b, c, d ) and any value of a single parameter of c or d, the producer is not sure which function to use. As a result, it generates uncertainty, which leads to the mistake.


Related Topics

C++ Namespaces

An Overview In each scope, a name can only represent one entity. As a result, there cannot be two independent variables with the similar names in the same scope, as this may cause...

10 minutes read.

C++ Friend Functions

In C++, friends are special functions that are not a part of a class yet have access to its private and protected members. The friend keyword is used to declare...

4 minutes read.

C++ Output Iterators

Iterators : Iterators serve as a link between algorithms and STL containers, allowing the data inside the container to be modified. They let you to iterate through the container, access and...

4 minutes read.

C++ Multimap

Definition: In C++, a multimap is similar to a map with the additional concept, where multiple elements possess the same keys. It is also not necessary that the key values and...

4 minutes read.

Include Guards in C++

In C++ programming, we frequently utilize a class more than once, so it is necessary to create a header file and include it in the main program. Now, occasionally a...

3 minutes read.

C++ Nested if

C++'s nested if statements enable more complex decision-making when a section of code needs to execute only after a set of conditions is met. The nested if control statement refers...

4 minutes read.

Stack in C++

Stack: The stack is a very popular data structure. It is the form of data structure that follows a particular order called FIFO(First-In-First-Out). In simple words, a stack is an Abstract...

4 minutes read.

Print Table Using For-loop in C++

Multiplication Table In mathematics, a table is created by multiplying a certain number by all of the counting numbers, i.e., 1, 2, 3, 4, 5, 6, and so on. It is...

3 minutes read.

C++ Type Casting

The type casting of variables in the C++ programming language will be covered in this section. The term "type casting" describes how software converts one data type to another. There...

9 minutes read.

Initialize Vector in C++

Initialize Vector in C++  The following comparison operators are defined for vector and those are given below. ==, <, <=, !=, >,>=  This allows you to access the element of a vector using...

3 minutes read.

C++ Deque

Definition: Deque or the Doubly ended queue is a data structure or operation performed under queue where insertion and deletion are allowed at both ends. A deque is an ordered collection of...

5 minutes read.

Diamond Pattern using While-loop in C++

What is while Loop? A while loop or while statement repeats all code of its body as long as a specific condition is satisfied. The loop ends if or when the...

5 minutes read.

Do-While Loop Examples in C++

Before we move on the examples of Do-While loop, let’s learn little bit about the Do-While loop in C++ language. Do While loop An iterative loop that checks the condition at the...

5 minutes read.

Palindrome Number Program in C++

A palindrome number is one that is the same when it is reversed. Palindrome numbers include 22, 33, 44, 55, 66, 77, 88, and 99. Algorithm for Palindrome Numbers Get the user's...

4 minutes read.

Snake Code in C++

Snake is a popular game that can be played on almost any device and runs on any operating system. In this game, snakes can move in any direction, including left,...

4 minutes read.

LCM Program in C++

What is LCM? LCM is an acronym for "least common multiple". It is used to discover the lowest positive integer divisible by all integers (whose LCM is calculated). For instance, the...

4 minutes read.

Backtracking Time Complexity in C++

Introduction to Backtracking Backtracking is an important part of programming languages, and with the help of backtracking, we can do many operations in an advanced data structure. For doing major operations,...

4 minutes read.

Print Table Using Do while Loop in C++

Multiplication Table In mathematics, a table is created by multiplying a certain number by all of the counting numbers, i.e., 1, 2, 3, 4, 5, 6, and so on. It is...

4 minutes read.

C++ this pointer

'this' is a pointer that points to the object for which this function was called. The 'this' pointer holds the memory address of the current object. The 'this' pointer is implicitly passed to...

2 minutes read.

Malloc() and new in C++

In C ++, malloc () and new are used for the same thing. During runtime, they are used to allocate memory. Malloc () and the new, on the other hand,...

4 minutes read.