Course Content
C++ Introduction
0/1
C++ Variables & Constants
0/1
C++Scope of Variable
0/1
C++ Keywords & Identifiers
0/1
C++ Data Types
0/1
C++ Basic I/O
0/1
C++ Type Conversion
0/1
C++ Operators
0/1
C++ Comments
0/1
C++ If-else
0/1
C++ Ternary Operator
0/1
C++ for Loop
0/1
C++ Ranged for Loop
0/1
C++ while/do-while Loop
0/1
C++ break Statement
0/1
C++ Continue Statement
0/1
C++ switch Statement
0/1
C++ goto Statement
0/1
C++ Functions
0/1
C++ User-defined Functions
0/1
C++ Default Arguments
0/1
C++ Storage Class
0/1
C++ Recursion
0/1
C++ Return by Reference
0/1
C++ Arrays
0/1
C++ Multi-dimentional Arrays
0/1
C++ Arrays & Function
0/1
C++ String
0/1
C++ Structure
0/1
C++ Structure & Functions
0/1
C++ Pointers to Structure
0/1
C++ Pointers
0/1
C++ Void Pointers
0/1
C++ Pointers & Arrays
0/1
C++ Pointers & Functions
0/1
C++ Dynamic Memory Allocation
0/1
C++ OOPs Concepts
0/1
C++ Objects and Class
0/1
C++ Constructors
0/1
C++ Destructors
0/1
C++ Constructor Overloading
0/1
C++ Objects & Function
0/1
C++ Enumeration
0/1
C++ Inheritance
0/1
C++ Inheritance Access Control
0/1
C++ Inheritance Types
0/1
C++ Polymorphism
0/1
C++ Function Overloading
0/1
C++ Function Overriding
0/1
C++ Operator Overloading
0/1
C++ Friend Function
0/1
C++ Virtual Function
0/1
C++ Abstract Class & Pure Virtual Function
0/1
C++ Encapsulation
0/1
C++ Abstraction
0/1
C++ Templates
0/1
C++ Exception Handling
0/1
C++ Multithreading
0/1
C++ Standard Library
0/1
C++ Programming Tutorials
About Lesson

C++ Default Arguments

In this tutorial, you will learn about different approaches you can take to solve a single problem using functions.


Default Arguments

  • In C++ programming, we can provide default values for function parameters.
  • A default argument is a value provided in a fuction declaration that is automatically assigned by the compiler if the caller of the function doesn’t provide a value for the argument with a default value.
  • A default argument is a function argument that has a default value provided to it.
  • If the user does not provide a value for this argument, the default value will be used.
  • If the user does not provide a value for the default argument, the user- supplied value is used.

If a function with default arguments is called without passing arguments, then the default parameters are used.

However, if arguments are passed while calling the function, the default arguments are ignored.


Example 1: Default Argument

// This program to check if you are 18 or not using goto statement.

# include <iostream>
using namespace std;
                   
// defining the default arguments
void character(char = 'a', int = 3);
                                  
int main() {
    int count = 4;
                                  
    cout << "No argument passed: ";
    // a, 2 will be parameters
    character(); 
                                      
    cout << "First argument passed: ";
     // b, 3 will be parameters
    character('b'); 
                                      
    cout << "Both arguments passed: ";
    // c, 4 will be parameters
    character('c', count); 
                                  
    return 0;
}
                                  
void character(char d, int count) {
    for(int i = 1; i <= count; ++i)
    {
        cout << d;
    }
    cout << endl;
}

Output 1

No argument passed: aaa
First argument passed: bbb
Both arguments passed: cccc

Here is how this program works:

  1. character() is called without passing any arguments. In this case, character() uses both the default parameters d = 'a' and n = 1.
  2. character('c') is called with only one argument. In this case, the first becomes 'c'. The second default parameter n = 1 is retained.
  3. character('c', count) is called with both arguments. In this case, default arguments are not used.

We can also define the default parameters in the function definition itself. The program below is equivalent to the one above.


Example 2: Function with No arguments passed but a return value

#include <iostream>
using namespace std;
// defining the default arguments

void character(char d = 'a', int count = 3) {
for(int i = 1; i <= count; ++i) {
        cout << d;
    }
        cout << endl;
}
                              
int main() {
    int count = 4;
                                  
    cout << "No argument passed: ";
    // a, 2 will be parameters
    character(); 
                                      
    cout << "First argument passed: ";
    // b, 3 will be parameters    
    character('b'); 
                                      
    cout << "Both argument passed: ";
    // c, 4 will be parameters
    character('c', count); 
                                  
    return 0;
}

Output 1

No argument passed: aaa
First argument passed: bbb
Both arguments passed: cccc

Key Points

  • Default arguments are different from constant arguments as constant arguments can’t be changed whereas default arguments can be overwritten if required.
  • Default arguments are overwritten when calling function provides values for them.
  • During calling of function, arguments from calling function to called function are copied from left to right.
  • Once default value id used for an argument in function definition, all subsequent arguments to it must have default value.
  • It can also be stated as default arguments are assigned from right to left.

Important Points

  1. Once we provide a default value for a parameter, all subsequent parameters must also have default values. For example,
    // Invalid
    void sum(int a, int b = 2, int c, int d);
                          
    // Invalid
    void sum(int a, int b = 2, int c, int d = 4);
                          
    // Valid
    void sum(int a, int c, int b = 2, int d = 4);
  2. If we are defining the default arguments in the function definition instead of the function prototype, then the function must be defined before the function call.
    // Invalid code
                          
    int main() {
    // function call
    print();
    }
    
    void print(char c = '@', int count = 4) {
    // code
    }

Some Valid/Invalid default values are:

int sum (int a, int b, int c) Valid/Invalid
int sum (int a, int b=2, int c=3); Valid
int sum (int a=1, int b=2, int c=3); Valid
int sum (int a=1, int b, int c); Invalid
int sum (int a=1, int b, int c=3); Invalid

 

Exercise Files
No Attachment Found
No Attachment Found
error: Content is protected !!