Default argument


In computer programming, a default argument is an argument to a function that a programmer is not required to specify.
In most programming languages, functions may take one or more arguments. Usually, each argument must be specified in full. Later languages allow the programmer to specify default arguments that always have a value, even if one is not specified when calling the function.

Default arguments in C++

Consider the following function declaration:

int MyFunc;

This function takes three arguments, of which the last one has a default of twelve. The programmer may call this function in two ways:

int result = MyFunc;
result = MyFunc;

In the first case the value for the argument called c is specified as normal. In the second case, the argument is omitted, and the default value of 12 will be used instead.
There is no means to know if the argument has been specified by the caller or if the default value was used.
The above-mentioned method is especially useful when one wants to set default criteria so that the function can be called with or without parameters.
Consider the following:

void PrintGreeting

The function call:

PrintGreeting;

will by default print "hello world!" to the standard output std::cout. On the other hand, any object of type std::ostream can now be passed to the same function and the function will print to the given stream instead of to the standard output.

PrintGreeting;

Because default arguments' values are "filled in" at the call site rather than in the body of the function being called, virtual functions take their default argument values from the static type of the pointer or reference through which the call is made, rather than from the dynamic type of the object supplying the virtual function's body.

struct Base ;
struct Derived : public Base ;
int main

Overloaded methods

Some languages, such as Java, do not have default arguments. However, the same behaviour can be simulated by using method overloading to create overloaded methods of the same name, which take different numbers of arguments; and the versions with fewer arguments simply call the versions with more arguments, with the default arguments as the missing arguments:

int MyFunc
int MyFunc

However, in addition to , since the default arguments are not modeled in the type system, the type of a callback can’t express that it accepts either of the overloads nor simulate the default arguments with overloaded functions. Whereas, the non-overloaded function definition can substitute the default when the input value is undefined ; which is modeled as an optional argument parameter type ?: . JavaScript’s solution is thus incurs additional runtime overhead, although it does provide more flexibility in that callbacks can independently control their defaults instead of centrally dictated by the type signature of the function which inputs the callback. The TypeScript solution can be with the Optional type except the analogous of an implicit undefined for each absent argument is an explicit Optional.absent at the call site.

Evaluation

For every function call default argument values must be passed to the called function.
If a default argument value contains side-effects, it is significant when those side effects are evaluated – once for the entire program, or once per function call, at call time.
Python is a notable language that evaluates expressions in default arguments once, at the time the function declaration is evaluated. If evaluation per function call is desired, it can be replicated by having the default argument be a sentinel value, such as None, and then having the body of the function evaluate the default value's side effects only if the sentinel value was passed in.
For example:

import random
def eager):
return a
x = eager
y = eager
assert x y
def lazy:
if a is None:
a = random.random
return a
x = lazy
y = lazy
assert x != y

Extent

Generally a default argument will behave identically to an argument passed by parameter or a local variable declared at the start of the function, and have the same scope and extent as a parameter or other local variable, namely an automatic variable which is deallocated on function termination.
In other cases a default argument may instead be statically allocated. If the variable is mutable, it will then retain its value across function calls, as with a static variable.
This behavior is found in Python for mutable types, such as lists. As with evaluation, in order to ensure the same extent as a local variable, one can use a sentinel value:

def eager:
return a
x = eager
x +=
assert eager
def lazy:
if a is None:
a =
return a
x = lazy
x +=
assert lazy