Software Development Exam  >  Software Development Notes  >  Basics of C++  >  Default Arguments in C++

Default Arguments in C++ | Basics of C++ - Software Development PDF Download

Introduction

In C++, a function argument can be given a default value, which means that if the caller doesn't provide a value for that argument, the default value will be used. This feature is called "default arguments." Default arguments are useful when you want to provide a default behavior for a function, but still allow the caller to override it if necessary.

Syntax

The syntax for default arguments in C++ is as follows:

return_type function_name(parameter1=default_value1, parameter2=default_value2, ..., parameterN=default_valueN)

{

    // Function body

}

Explanation

  • 'return_type' is the type of the value returned by the function.
  • 'function_name' is the name of the function.
  • 'parameter1', 'parameter2', ..., 'parameterN' are the function parameters.
  • 'default_value1', 'default_value2', ..., 'default_valueN' are the default values for the parameters.

Example:

#include <iostream>


void print(int a, int b=10, int c=20)

{

    std::cout << "a=" << a << ", b=" << b << ", c=" << c << std::endl;

}


int main()

{

    print(1);         // Output: a=1, b=10, c=20

    print(2, 30);     // Output: a=2, b=30, c=20

    print(3, 40, 50); // Output: a=3, b=40, c=50

    

    return 0;

}

Explanation

  • In this example, we define a function 'print' that takes three integer parameters, 'a', 'b', and 'c'.
  • The default values for 'b' and 'c' are '10' and '20', respectively.
  • When the 'print' function is called with only one argument, 'a' takes the value of the argument, and 'b' and 'c' take their default values.
  • When the 'print' function is called with two arguments, 'a' takes the value of the first argument, 'b' takes the value of the second argument, and 'c' takes its default value.
  • When the 'print' function is called with three arguments, 'a', 'b', and 'c' take the values of the three arguments.

Advantages

Default arguments provide the following advantages:

  • They simplify the function call by allowing the caller to omit some parameters.
  • They provide default behavior for the function.
  • They make the function more flexible by allowing the caller to override the default behavior.

Disadvantages

Default arguments have some limitations:

  • They can make the function signature less clear and harder to understand.
  • They can lead to ambiguity if there are multiple functions with the same name but different default arguments.

Sample Problems:

1. Write a C++ function that calculates the volume of a cube with default edge length of 1 unit.

#include <iostream>


double cubeVolume(double edge=1.0)

{

    return edge * edge * edge;

}


int main()

{

    std::cout << "Volume of a cube with edge length 2 units: " << cubeVolume(2.0) << std::endl;

    std::cout << "Volume of a cube with default edge length: " << cubeVolume() << std::endl;


    return 0;

}

Output

Volume of a cube with edge length 2 units: 8

Volume of a cube with default edge length: 1

Explanation: In this example, we define a function 'cubeVolume' that takes one double parameter, `edge2. Write a C++ function that calculates the factorial of a given number with a default argument of 1.

#include <iostream>


int factorial(int n, int result=1)

{

    if (n == 0 || n == 1)

    {

        return result;

    }

    else

    {

        return factorial(n-1, result*n);

    }

}


int main()

{

    std::cout << "Factorial of 5: " << factorial(5) << std::endl;

    std::cout << "Factorial of 6 with initial value of 10: " << factorial(6, 10) << std::endl;


    return 0;

}

Output

Factorial of 5: 120

Factorial of 6 with initial value of 10: 7200

Explanation

  • In this example, we define a function 'factorial' that takes one integer parameter 'n' and an optional integer parameter 'result', which has a default value of 1.
  • The function calculates the factorial of 'n' using recursion.
  • The optional 'result' parameter keeps track of the current result of the multiplication.
  • When the recursion is finished, the function returns the final result.

Conclusion

Default arguments are a powerful feature of C++ that can simplify the function call and provide default behavior for the function. They have some limitations, but they are generally useful and widely used in C++ programming.

The document Default Arguments in C++ | Basics of C++ - Software Development is a part of the Software Development Course Basics of C++.
All you need of Software Development at this link: Software Development
70 videos|44 docs|15 tests
70 videos|44 docs|15 tests
Download as PDF
Explore Courses for Software Development exam
Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Download the FREE EduRev App
Track your progress, build streaks, highlight & save important lessons and more!
Related Searches

Default Arguments in C++ | Basics of C++ - Software Development

,

Important questions

,

mock tests for examination

,

Default Arguments in C++ | Basics of C++ - Software Development

,

shortcuts and tricks

,

Semester Notes

,

past year papers

,

Summary

,

video lectures

,

study material

,

Exam

,

Free

,

pdf

,

Extra Questions

,

MCQs

,

Default Arguments in C++ | Basics of C++ - Software Development

,

Viva Questions

,

Sample Paper

,

ppt

,

practice quizzes

,

Previous Year Questions with Solutions

,

Objective type Questions

;