Default argument
This article does not cite any references or sources. Please help improve this article by adding citations to reliable sources. Unsourced material may be challenged and removed. (May 2009) |
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 (this is the case in the C programming language[1]).
Later languages (for example, in C++) allow the programmer to specify default arguments that always have a value, even if one is not specified when calling the function. For example, in the following function declaration:
int MyFunc(int a, int b, int c=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:
result = MyFunc(1, 2, 3);
result = MyFunc(1, 2);
In the first case the value for the argument called c is specified as normal. In the second one, 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.
Some other languages, like Java, do not have default arguments. However, the same behavior 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 a, int b) { return MyFunc(a, b, 12); }
int MyFunc(int a, int b, int c) { /* main implementation here */ }
File:HelloWorld.svg | This programming language-related article is a stub. You can help Wikipedia by expanding it. |
References
External links
fa:آرگومان پیشفرض nl:Standaardargument pl:Argument domyślny
If you like SEOmastering Site, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...