This articleneeds additional citations forverification. Please helpimprove this article byadding citations to reliable sources. Unsourced material may be challenged and removed. Find sources: "Default argument" – news ·newspapers ·books ·scholar ·JSTOR(May 2009) (Learn how and when to remove this message) |
Incomputer programming, adefault argument is anargument to afunction 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 (this is the case in theC programming language[1]). Later languages (for example, inC++) allow the programmer to specify default arguments that always have a value, even if one is not specified when calling the function.
Consider the following function declaration:
intMyFunc(inta,intb,intc=12);
This function takes three arguments, of which the last one has a default of twelve. The programmer may call this function in two ways:
intresult=MyFunc(1,2,3);result=MyFunc(1,2);
In the first case the value for the argument calledc is specified explicitly. In the second case, the argument is omitted, and the default value of12 will be used instead.For the function called, 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:
voidPrintGreeting(std::ostream&stream=std::cout){// This outputs a message to the given stream.stream<<"hello world!";}
The function call:
PrintGreeting();
will by default print "hello world!" to thestandard outputstd::cout (typically the screen). On the other hand, any object of typestd::ostream can now be passed to the same function and the function will print to the given stream instead of to the standard output. The example below sets thestd::ostream& tostd::cerr, and thus prints the output the standard error stream.
PrintGreeting(std::cerr);
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.
structBase{virtualstd::pair<int,int>Foo(intx=1){return{x,1};}};structDerived:publicBase{std::pair<int,int>Foo(intx=2)override{return{x,2};}};intmain(){Derivedd;Base&b=d;assert(d.Foo()==std::make_pair(2,2));assert(b.Foo()==std::make_pair(1,2));}
Some languages, such asJava, do not have default arguments. However, the same behaviour can be simulated by usingmethod 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:
intMyFunc(inta,intb){returnMyFunc(a,b,12);}intMyFunc(inta,intb,intc){/* main implementation here */}
However, in addition toseveral other disadvantages, since the default arguments are not modeled in the type system, the type of a callback (akahigher-order function) can't express that it accepts either of the overloads nor simulate the default arguments with overloaded functions. Whereas,in JavaScript the non-overloaded function definition can substitute the default when the input value isundefined (regardless if it was implicitlyundefined via the argument's absence at the call site or an explicitly passedundefined value); which is modeled as an optional argument parameter type?:in TypeScript. JavaScript's solution isnot resolved statically (i.e. not at compile-time, which is why TypeScript models only the optionality and not the default values in the function'stype signature) 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 (callback's type signature in the) type signature of the function which inputs the callback. The TypeScript solution can besimulated in Java with theOptional type except the analogous of an implicitundefined for each absent argument is an explicitOptional.<Integer>absent() at the call site.
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 (at parse time, compile time, or load time), 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 asentinel value, such asNone, 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:
importrandomdefeager(a=random.random()):returnax=eager()y=eager()assertx==ydeflazy(a=None):ifaisNone:a=random.random()returnax=lazy()y=lazy()assertx!=y
Generally a default argument will behave identically to an argument passed by parameter or alocal variable declared at the start of the function, and have the samescope and extent (lifetime) as a parameter or other local variable, namely anautomatic 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 astatic 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:
defeager(a=[]):returnax=eager()x+=[1]asserteager()==[1]deflazy(a=None):ifaisNone:a=[]returnax=lazy()x+=[1]assertlazy()==[]