Default argument
Encyclopedia
In computer programming
, a default argument is an argument to a function that a programmer is not required to specify. A default value is specified when the function is declared and the default value is automatically passed to the function when it is called. Default argument is a part of a function declaration and it tells compiler
to pass the value for an argument if programmer deliberately misses an argument while calling the function. In the common case that many arguments are almost always the same. This saves programmer's time and effort required to remember and specify all arguments. Default arguments are useful in situations where some arguments always have the same value. For instance, bank interest may remain same for all customers for a particular period of deposit. It also provides greater flexibility to programmers.
allows to specify default arguments. In C++ the function assigns a default value to the 'parameter' which does not have a matching argument in function call. Default values are specified when the function is declared. The compiler
looks at the prototype to see how many arguments a function uses though the programmer assigns less value of the arguments than declared at the time of function call and compiler
alerts the program for possible default values.
Here rate is declared as a default parameter, with default value 0.05.
The function calcInterest is written with only two arguments but the third will get passed automatically. The compiler memorizes the default argument so it knows it can still make the function call if it substitutes this third argument which is what user have told it to do by making it a default.
Here principal gets the value 123.45, period gets the value 6 and rate gets the (default) value 0.05.
calcInterest may also be called with three arguments overriding the default rate:
Output of the above program is-
Computer programming
Computer programming is the process of designing, writing, testing, debugging, and maintaining the source code of computer programs. This source code is written in one or more programming languages. The purpose of programming is to create a program that performs specific operations or exhibits a...
, a default argument is an argument to a function that a programmer is not required to specify. A default value is specified when the function is declared and the default value is automatically passed to the function when it is called. Default argument is a part of a function declaration and it tells compiler
Compiler
A compiler is a computer program that transforms source code written in a programming language into another computer language...
to pass the value for an argument if programmer deliberately misses an argument while calling the function. In the common case that many arguments are almost always the same. This saves programmer's time and effort required to remember and specify all arguments. Default arguments are useful in situations where some arguments always have the same value. For instance, bank interest may remain same for all customers for a particular period of deposit. It also provides greater flexibility to programmers.
Variation among languages
Most of the Object Oriented Programming (list of Object Oriented Programming languages) languages allow to use default arguments. Variation in the syntax is seen in these languages. Languages like C++, Python and Java etc. are popular in the world of computer programming.C++
C++C++
C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...
allows to specify default arguments. In C++ the function assigns a default value to the 'parameter' which does not have a matching argument in function call. Default values are specified when the function is declared. The compiler
Compiler
A compiler is a computer program that transforms source code written in a programming language into another computer language...
looks at the prototype to see how many arguments a function uses though the programmer assigns less value of the arguments than declared at the time of function call and compiler
Compiler
A compiler is a computer program that transforms source code written in a programming language into another computer language...
alerts the program for possible default values.
Usage with syntax
Here is an example of a function prototype (i.e., function declaration) with a default argument:Here rate is declared as a default parameter, with default value 0.05.
The function calcInterest is written with only two arguments but the third will get passed automatically. The compiler memorizes the default argument so it knows it can still make the function call if it substitutes this third argument which is what user have told it to do by making it a default.
Here principal gets the value 123.45, period gets the value 6 and rate gets the (default) value 0.05.
calcInterest may also be called with three arguments overriding the default rate:
Caution
One important point to note is that only trailing arguments can have default values and therefore while declaring the function defaults must be added from right to left.Illustration with program
Output of the above program is-