How to pass optional arguments to a method in C++ ? Any code snippet...
-
15You don't pass option parameters. You pass optional arguments! – Chubsdad Sep 24 '10 at 04:57
-
For more explicit control than that provided by reserving sentinel values, check out boost::optional<>. – Tony Delroy Sep 24 '10 at 06:54
9 Answers
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

- 3,249
- 3
- 24
- 42

- 14,667
- 4
- 33
- 34
-
-
-
2`NULL` means a NULL pointer, even though it would be defined as literal `0`. It is not a universal name for constant zero. For integers (non-pointers) you should use numbers: `int mode = 0`. – UncleBens Sep 24 '10 at 04:53
-
1If you're working with a class (source and header files), where would you define the default value? – AceFunk Dec 21 '17 at 12:34
-
3@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 – Mars May 08 '18 at 08:18
-
What if I don't know if a user would like to pass 0 or any other number? I need something that is not an int – ניר May 24 '23 at 09:08
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
-
@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" – Alok Save Sep 24 '10 at 05:07
-
1So 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? – Gerard Apr 24 '14 at 10:19
-
@Gerard: The "Allowed" example shows one optional parameter and not the rest use case which is valid. – Alok Save Apr 24 '14 at 16:25
-
6I understand, but what if I were to have `int foo(int x, int y = 10, int z = 10)` and would want to call `foo(1,2)`, so only giving one optional parameter. I did not seem to be able to get it to work myself. – Gerard Apr 24 '14 at 16:30
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/

- 680
- 6
- 10
-
4This 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 – Teh Sunn Liu Oct 27 '16 at 01:57
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();
}

- 549
- 4
- 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

- 417
- 4
- 6
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 )

- 91,295
- 49
- 239
- 345
-
Thanks for the general solution, I was trying to figure out how to provide a default value for any arbitrary type. – pretzlstyle May 14 '18 at 19:59
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

- 277,958
- 43
- 419
- 720
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;
}

- 476,176
- 80
- 629
- 1,111
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

- 43
- 1
- 9
-
This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/31254837) – Ryan Pendleton Mar 13 '22 at 22:46