Questions tagged [default-arguments]

A default argument is a value passed to a certain parameter of a function if the user does not explicitly supply a value for that parameter.

A default argument is a value passed to a certain parameter of a function if the user does not explicitly supply a value for that parameter.

Some language extend this concept to other entities that have parameters. An example is C++, which has default template arguments, which is a value, type or template that is provided as an argument to a template parameter if the user does not explicitly provide an argument.

356 questions
340
votes
10 answers

Where to put default parameter value in C++?

What's the place for the default parameter value? Just in function definition, or declaration, or both places?
Thomson
  • 20,586
  • 28
  • 90
  • 134
229
votes
4 answers

Default value in Go's method

Is there a way to specify default value in Go's function? I am trying to find this in the documentation but I can't find anything that specifies that this is even possible. func SaySomething(i string = "Hello")(string){ ... }
denniss
  • 17,229
  • 26
  • 92
  • 141
146
votes
2 answers

error: default argument given for parameter 1

I'm getting this error message with the code below: class Money { public: Money(float amount, int moneyType); string asString(bool shortVersion=true); private: float amount; int moneyType; }; First I thought that default parameters…
pocoa
  • 4,197
  • 9
  • 37
  • 45
136
votes
8 answers

Good uses for mutable function argument default values?

It is a common mistake in Python to set a mutable object as the default value of an argument in a function. Here's an example taken from this excellent write-up by David Goodger: >>> def bad_append(new_item, a_list=[]): …
Jonathan Livni
  • 101,334
  • 104
  • 266
  • 359
95
votes
6 answers

How can I use an attribute of the instance as a default argument for a method?

I want to pass a default argument to an instance method using the value of an attribute of the instance: class C: def __init__(self, format): self.format = format def process(self, formatting=self.format): …
Yugal Jindle
  • 44,057
  • 43
  • 129
  • 197
66
votes
7 answers

How to use source_location in a variadic template function?

The C++20 feature std::source_location is used to capture information about the context in which a function is called. When I try to use it with a variadic template function, I encountered a problem: I can't see a place to put the source_location…
L. F.
  • 19,445
  • 8
  • 48
  • 82
54
votes
2 answers

Default argument in the middle of parameter list?

I saw a function declaration in our code that looked as follows void error(char const *msg, bool showKind = true, bool exit); I thought first that this is an error because you cannot have default arguments in the middle of functions, but the…
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
47
votes
5 answers

Missing default argument - compiler error

void func ( string word = "hello", int b ) { // some jobs } in another function //calling func ( "", 10 ) ; When I have compiled it, compiler emits error ; default argument missing for parameter How can I fix it without changing…
user478571
46
votes
2 answers

Function default argument value depending on argument name in C++

If one defines a new variable in C++, then the name of the variable can be used in the initialization expression, for example: int x = sizeof(x); And what about default value of a function argument? Is it allowed there to reference the argument by…
43
votes
2 answers

lambda *args, **kwargs: None

consider: blank_fn = lambda *args, **kwargs: None def callback(x, y, z=''): print x, y, z def perform_task(callback=blank_fn): print 'doing stuff' callback('x', 'y', z='z' ) The motivation for doing it this way is I don't have to put…
Scruffy
  • 908
  • 1
  • 8
  • 21
36
votes
2 answers

In std::exchange, why is the second template parameter defaulted?

The C++14 standard specifies the following declaration for std::exchange: template T std::exchange(T& obj, U&& new_value); I am wondering why U is defaulted to T since U can be found thanks to new_value. In what case, this…
Vincent
  • 57,703
  • 61
  • 205
  • 388
36
votes
4 answers

What if the lambda expression of C++11 supports default arguments?

I think the following code is very handy and no harmful: auto fn = [](bool b = false) -> int // NOT legal in C++11 { return b ? 1 : 0; }; Why does C++11 explicitly prohibit default arguments of the lambda expression? I just wonder the…
xmllmx
  • 39,765
  • 26
  • 162
  • 323
33
votes
1 answer

std::map argument with empty brace-initializers for default argument segfaults in GCC

Problem I got a bug report from user reporting a segfault in library I develop. The minimal example of the faulty code is: #include #include #include void f(std::map m = {}) { std::cout <<…
v154c1
  • 1,698
  • 11
  • 19
30
votes
2 answers

Can C++ deduce argument type from default value?

I tried to write this function with a default template argument: template void func(int i1, int i2, A a, B b = 123){ ... } In my mind I can call it like this: func(1, 2, 3) and compiler should deduce type B as int from…
28
votes
4 answers

Can I bind to a function that takes default arguments and then call it?

How can I bind to a function that takes default arguments, without specifying the default arguments and then call it without any arguments? void foo(int a, int b = 23) { std::cout << a << " " << b << std::endl; } int main() { auto f =…
Stephan Dollberg
  • 32,985
  • 16
  • 81
  • 107
1
2 3
23 24