How to pass optional arguments to a method in C++?

C++Optional Arguments

C++ Problem Overview


How to pass optional arguments to a method in C++ ? Any code snippet...

C++ Solutions


Solution 1 - C++

Here is an example of passing mode as optional parameter

void myfunc(int blah, int mode = 0)
{
    if (mode == 0)
        do_something();
     else
        do_something_else();
}

you can call myfunc in both ways and both are valid

myfunc(10);     // Mode will be set to default 0
myfunc(10, 1);  // Mode will be set to 1

Solution 2 - C++

An important rule with respect to default parameter usage:
Default parameters should be specified at right most end, once you specify a default value parameter you cannot specify non default parameter again. ex:

int DoSomething(int x, int y = 10, int z) -----------> Not Allowed

int DoSomething(int x, int z, int y = 10) -----------> Allowed 

Solution 3 - C++

It might be interesting to some of you that in case of multiple default parameters:

void printValues(int x=10, int y=20, int z=30)
{
    std::cout << "Values: " << x << " " << y << " " << z << '\n';
}

Given the following function calls:

printValues(1, 2, 3);
printValues(1, 2);
printValues(1);
printValues();

The following output is produced:

Values: 1 2 3
Values: 1 2 30
Values: 1 20 30
Values: 10 20 30

Reference: http://www.learncpp.com/cpp-tutorial/77-default-parameters/

Solution 4 - C++

To follow the example given here, but to clarify syntax with the use of header files, the function forward declaration contains the optional parameter default value.

myfile.h

void myfunc(int blah, int mode = 0);

myfile.cpp

void myfunc(int blah, int mode) /* mode = 0 */
{
    if (mode == 0)
        do_something();
     else
        do_something_else();
}

Solution 5 - C++

Use default parameters

template <typename T>
void func(T a, T b = T()) {

   std::cout << a << b;

}

int main()
{
    func(1,4); // a = 1, b = 4
    func(1);   // a = 1, b = 0

    std::string x = "Hello";
    std::string y = "World";
    
    func(x,y);  // a = "Hello", b ="World"
    func(x);    // a = "Hello", b = "" 
    
}

Note : The following are ill-formed

template <typename T>
void func(T a = T(), T b )

template <typename T>
void func(T a, T b = a )

Solution 6 - C++

With the introduction of std::optional in C++17 you can pass optional arguments:

#include <iostream>
#include <string>
#include <optional>

void myfunc(const std::string& id, const std::optional<std::string>& param = std::nullopt)
{
    std::cout << "id=" << id << ", param=";
    
    if (param)
        std::cout << *param << std::endl;
    else
        std::cout << "<parameter not set>" << std::endl;
}

int main() 
{
    myfunc("first");
    myfunc("second" , "something");
}

Output:

id=first param=<parameter not set>
id=second param=something

See https://en.cppreference.com/w/cpp/utility/optional

Solution 7 - C++

With commas separating them, just like parameters without default values.

int func( int x = 0, int y = 0 );

func(); // doesn't pass optional parameters, defaults are used, x = 0 and y = 0

func(1, 2); // provides optional parameters, x = 1 and y = 2

Solution 8 - C++

Typically by setting a default value for a parameter:

int func(int a, int b = -1) { 
    std::cout << "a = " << a;
    if (b != -1)        
        std::cout << ", b = " << b;
    std::cout << "\n";
}

int main() { 
    func(1, 2);  // prints "a=1, b=2\n"
    func(3);     // prints "a=3\n"
    return 0;
}

Solution 9 - C++

Jus adding to accepted ans of @Pramendra , If you have declaration and definition of function, only in declaration the default param need to be specified

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSwapnil GuptaView Question on Stackoverflow
Solution 1 - C++Pramendra GuptaView Answer on Stackoverflow
Solution 2 - C++Alok SaveView Answer on Stackoverflow
Solution 3 - C++user2008151314View Answer on Stackoverflow
Solution 4 - C++KenJView Answer on Stackoverflow
Solution 5 - C++Prasoon SauravView Answer on Stackoverflow
Solution 6 - C++Fernando G. TestaView Answer on Stackoverflow
Solution 7 - C++Ben VoigtView Answer on Stackoverflow
Solution 8 - C++Jerry CoffinView Answer on Stackoverflow
Solution 9 - C++fadView Answer on Stackoverflow