×

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++ Signal Handling

Signals are interruptions sent by the operating system to a process to cause it to cease doing its current job and focus on the task for which the interrupt was...

4 minutes read.

sort() function in C++

This tutorial covers the various built-in sort functions found in the C++ algorithm’s library.  What Does C++ Sort Mean? The concept of sorting in C++ entails rearranging an array's elements in a...

3 minutes read.

Leap Year Program in C++

What is a Leap Year? A solar year is the length of time that it takes for Earth to orbit the Sun - approximately 365.25 days. In a calendar year, we...

4 minutes read.

C++ Scope of Variables

In this tutorial, we will explore about the scope of variables in c++ programming language. And also, how it works in a program. What is Scope? The range of applications for something...

4 minutes read.

Queue in C++

What is Queue? As the name suggests, the queue is the type of data structure that follows the FIFO (First In - First Out) mechanism. In simple words, it is...

4 minutes read.

C++ Program to find the element that occurs once

Write a program to find the element in the array that occurs once. Given that all the numbers in the array are present two times and the array is sorted,...

7 minutes read.

C++ If-else-if

Introduction: If-else-if control statement is an if statement used with an optional else if control statement to check multiple conditions. In this control statement, when any one of the condition returns...

4 minutes read.

Advanced C++ with Boost Library

The goal of the Boost Libraries is to be widely applicable and used in a variety of applications. For instance, they can in handy when working with huge numbers whose...

4 minutes read.

How is multiset implemented in C++

Similar to sets, multisets are an associative container type where several items may share the same values. Associative containers implement instantly searchable sorted data structures with O(log n) complexity. In a multiset,...

5 minutes read.

Processing strings using std string stream in C++

A string class object called std: string stream is used to streamline into various variables, just as files can pour into strings. This class's things make use of a string...

3 minutes read.

Depth First Search Program to Traverse a Graph in C++

Depth First Search (DFS) is a technique that is used for transversing the graph. The process of Depth First Search (DFS) starts from the root node and then next comes...

6 minutes read.

C++ Forward 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...

3 minutes read.

Array of Vectors in C++ STL

Prerequisites: C++ STL Arrays and C++ STL Vector. A group of items kept in consecutive memory region is known as an array. It is to group similar objects of the same...

4 minutes read.

Parameterize Constructor

C++ Parameterized Constructor A constructor having parameters is known as parameterize constructor. Parameterize constructor is used to assign different values. Syntax: className(data-type argument){   // Constructor definition   }   className(data-type argument, data-type argument){   // Constructor definition   } A parameterized constructor can be passed values to constructor function in two ways: 1)...

1 minute read.

How to concatenate two strings in C++

In the C++ programming language, the concatenation of two or even more strings is covered in this section. The term "string concatenation" refers to a collection of characters that join two...

4 minutes read.

Factorial Program in C++

C++ Factorial Program: The product of all positive descending integers is the factorial of n. n! denotes the factorial of n. For instance: 5! = 5*4*3*2*1=120 4! = 4*3*2*1=24 In Combinations and Permutations, the...

4 minutes read.

Structure Sorting (By Multiple Rules) in C++

To understand the concept of Structure Sorting (By Multiple Rules) in C++, it is recommended to know the Structures concept in the C++ programming language. Here the scenario is pretty...

3 minutes read.

C++ Structs

We frequently encounter scenarios in which we must store a bunch of data, whether of comparable or dissimilar data kinds. Arrays are used to hold a group of data of...

6 minutes read.

Splitting a string in C++

Any programming language must have the ability to work with string data. For programming needs, we sometimes need to separate string data. Many computer languages provide a split() method that...

4 minutes read.

How to call a void function in C++

Generally, any function has two types: 1. Void function: It doesn't return any value. 2. Non-void function: It returns some value. Program to call a void function in C++ #include <iostream> using namespace std;  void check() {  ...

2 minutes read.