--%>

Default function arguments

C++ allows us to call a function without specifying all its arguments. In such type of cases, the function allots a default value to the parameter which does not have a corresponding argument in the function call. Default values are specifies when the function is declared. The compiler looks at the proto type to see how many arguments a function uses and alerts the programme for possible default values. Here is an example of a proto type (function declarations) with default values:

Float amount (float principal, Int period, float rate = 0.15);

The default value is specified in a manner syntactically similar to a variable initialization. The above proto type declares a default value of 0.15 to the argument rate. A subsequent function call like

Value = amount (5000, 7);     // one argument missing

Passes the value of 5000 to principal and 7 to period and then less the function use default value of 0.15 for rate.

The call value = amount (5000, 5, 0.12);    // no missing argument

Passes an explicit value of 0.15 to rate.

  A default argument is checked for type at the time at the declaration and evaluated at the time of the call. One significant point to note down is that only the trailing arguments can own default values and thus we should add defaults from right to left. We cannot give a default value to an argument in the mid of an argument list. Some examples of function declarations with default values are:

Int mul (i, Int j = 5, Int k = 10);     // legal       

Int mul (i, Int j = 5, Int k = 10);     // legal      

Int mul (i, Int = 5, Int j );     // illegal      

Int mul (i, Int j = 5, Int k = 10);     // illegal

Default arguments are useful in situations where some arguments always have the same value. For example, bank interest may remain similar for all customers for a exact period of deposit. It also gives great flexibility to the programmers. A function can be written with more parameters than are required for its common applications. With the use of default arguments, a programmer can use only those arguments which are meaningful to an exact situation.  

 

   Related Questions in Programming Languages

  • Q : Define Peer Peer : It is a term

    Peer: It is a term employed of the Abstract Windowing Toolkit (AWT) to refer to the underlying classes which give the platform-specific implementation of the component classes.

  • Q : What is Bit Bit : It is a binary digit

    Bit: It is a binary digit that can take on two possible values: 0 and 1. The bits are basic building block of both data and programs. Computers regularly shift data around in multiples of eight-bit units (that is, bytes for the sake of effectiveness).

  • Q : Basic features of OOPs Illustrate the

    Illustrate the basic features of OOPs?

  • Q : Explain the differences between HTML

    Explain the differences between HTML and XML?

  • Q : What is Quotient Quotient: Whenever

    Quotient: Whenever integer division is executed, the outcome comprises of a quotient and a remainder. The quotient symbolizes the integer number of times which the divisor divides into the dividend. For example, in 5/3, 5 is the dividend and 3 is the

  • Q : Explain Out-of-bounds value

    Out-of-bounds value: It is a redundant value employed to point out that a different action from the norm is needed at some point. The read technique of InputStream returns -1 to point out that the end of a stream has been reached, for illustration, ra

  • Q : Assembly program that reads in five

     I need to write assembly program that reads in five numbers from the user. The user can then be prompted for one of the following pieces of information to be computed and returned: . the mean of the five numbers; . the largest number in the set; . the smallest number in the set; . t

  • Q : How can you explain basic elements of

    How can you explain basic elements of WebServices?

  • Q : Storing the CSS Definitions in external

    Describe the code in order to store CSS Definitions within the external Files.