How to pass optional arguments to a method in C++?
Solution 1
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
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
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
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
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
Swapnil Gupta
Updated on May 05, 2020Comments
-
Swapnil Gupta about 4 years
How to pass optional arguments to a method in C++ ? Any code snippet...
-
Swapnil Gupta almost 14 yearsCan you pls provide some example related to string ?
-
Pramendra Gupta almost 14 yearsvoid myfunc(int blah, char mode[] = NULL)
-
UncleBens almost 14 years
NULL
means a NULL pointer, even though it would be defined as literal0
. It is not a universal name for constant zero. For integers (non-pointers) you should use numbers:int mode = 0
. -
Alok Save almost 14 years@Chubsdad - Ahh..my bad ambigious statement! does the second statement sums it correctly? "once you specify a default value parameter you cannot specify non default parmeter again"
-
Gerard about 10 yearsSo if I understand correctly if I have multiple optional parameters I should either implement all of them or none at all? I cannot choose to use 1 optional parameter but not the rest?
-
Alok Save about 10 years@Gerard: The "Allowed" example shows one optional parameter and not the rest use case which is valid.
-
Gerard about 10 yearsI understand, but what if I were to have
int foo(int x, int y = 10, int z = 10)
and would want to callfoo(1,2)
, so only giving one optional parameter. I did not seem to be able to get it to work myself. -
Teh Sunn Liu over 7 yearsThis is what i was looking for. Use one function which can handle different number of arguments. Declare function with default value in header file then define it without default Parameters and then you can use it. No need of making function overload
-
AceFunk over 6 yearsIf you're working with a class (source and header files), where would you define the default value?
-
Mars about 6 years@AceFunk Super late, but the code I've seen has the default value defined in the header. If you think about it, the system wouldn't know that you can omit the value if the optional was defined in the source
-
pretzlstyle about 6 yearsThanks for the general solution, I was trying to figure out how to provide a default value for any arbitrary type.