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
15
votes
5 answers

Does PHP have "named parameters" so that early arguments can be omitted and later arguments can be written?

In PHP you can call a function with no arguments passed in so long as the arguments have default values like this: function test($t1 ='test1',$t2 ='test2',$t3 ='test3') { echo "$t1, $t2, $t3"; } test(); However, let's just say I want the last…
matthy
  • 8,144
  • 10
  • 38
  • 47
15
votes
2 answers

How should Scala default arguments to refer to a previous positional argument?

Scala-lang reference 5.5.1 and 6.6.1 gave me the impression that a default parameter would be able to refer to a previously evaluated one: class Test(val first: String, val second: String = first) but from experimenting it seems the only way to do…
15
votes
3 answers

Why does using this C++ function twice in one line cause a compile error?

I ran into some trouble trying to implement a smart equality test macro-type template function in Visual C++ 2010 that had to do with a bug in VS in regard to default arguments of template functions. I fixed it by wrapping the value of the parameter…
neuviemeporte
  • 6,310
  • 10
  • 49
  • 78
14
votes
5 answers

How to type mutable default arguments

The way to deal with mutable default arguments in Python is to set them to None. For example: def foo(bar=None): bar = [] if bar is None else bar return sorted(bar) If I type in the function definition, then the only type for bar says that…
Pro Q
  • 4,391
  • 4
  • 43
  • 92
14
votes
3 answers

Clang does not notice default template parameters

Background According to the C++ standard, when forward-declaring a template type with default template parameters, each of them can appear in one declaration only. For example: // GOOD example template class Example; //…
andreasxp
  • 174
  • 2
  • 9
13
votes
1 answer

C++ default arguments in function overload evaluated once or each time?

I have a question on defaults for overload functions in an object. If I have a function signature as follows will the default value be evaluated only once or each time? class X { public: f(const RWDate& d=RWDate::now()); } // when calling f()…
bjackfly
  • 3,236
  • 2
  • 25
  • 38
13
votes
1 answer

In C++, is a constructor with only default arguments a default constructor?

In the following code: struct Foo { Foo(int x=0); }; Does the constructor count as a default constructor?
Matt
  • 21,026
  • 18
  • 63
  • 115
12
votes
4 answers

Provide default arguments for subscript operator and function call operator

In the following code, I have provided default arguments for array subscript operator. struct st { int operator[](int x = 0) { // code here } }; But, compiler generated an error : error: 'int st::operator[](int)' cannot have…
msc
  • 33,420
  • 29
  • 119
  • 214
12
votes
1 answer

Dependent method types conflict with default arguments

When playing with scala's dependent method types, I encountered a conflict with default method parameters: abstract class X { type Y case class YY(y: Y) } object XX extends X { type Y = String } trait SomeTrait { def method(x: X)(y: x.YY,…
11
votes
3 answers

Is it possible to use default arguments and a variable number of arguments in a single function?

For example, something like this: #include void my_function(int it=42, ...) { /* va_list/start/arg/end code here */ } What exactly does the above code mean in C++? It compiles fine in G++. Note, I can't imagine any scenario where…
Alessandro
  • 113
  • 4
11
votes
1 answer

Verifying mocked object method calls with default arguments

Suppose I have this class: class Defaults { def doSomething(regular: String, default: Option[String] = None) = { println(s"Doing something: $regular, $default") } } I want to check that some other class invokes doSomething() method on…
Vladimir Matveev
  • 120,085
  • 34
  • 287
  • 296
10
votes
3 answers

How to add void/null as default argument to a function/lambda pointer, in C++?

Present signature is template bool isPrime(const TypeData& n,TypeFunc fSqrt,bool debug = false) and this works perfectly with std::cout<<(isPrime(n,fSqrt)?"Positive":"Negative")<<'\n'; But, my intension is…
10
votes
2 answers

How do I use overloaded functions with default arguments in algorithms?

I know the answer to the frequently-asked How do I specify a pointer to an overloaded function?: Either with assignment or with a cast, and every other C++ tutorial uppercases a string like this (give or take static_cast): transform(in.begin(),…
Cubbi
  • 46,567
  • 13
  • 103
  • 169
10
votes
2 answers

Kotlin: how to make a function call using the first argument's default value and passing a value for the second?

I'd like to know how to call a function with default arguments when you want to specify the value of the second argument. In the simple example below I am showing that the addTwo() takes two arguments. The 'first' argument has a default value but…
hlupico
  • 169
  • 1
  • 2
  • 9
10
votes
2 answers

Using rvalue references for default arguments

I want to make a function that takes an optional reference to an object, and creates one for the duration of the function if it is not provided, i.e. void Foo(Bar& b = Bar()) { /* stuff */ } This is, of course, invalid code, as a Bar cannot be…
1 2
3
23 24