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 : Function prototypes Function prototypes

    Function prototypes: Function declaration which specifies the function name, return type and parameter list of the function. Syntax: return_type function_name(type var1, type var2,…

  • Q : What is Break statement Break statement

    Break statement: A statement employed to break out of a loop, switch statement or labeled block. In all situations, control continues with the statement instantly, subsequent to the containing block.

  • Q : Computer science 1. Here is a short

    1. Here is a short program. It prints out the value of a variable "x". Ernie and Bert disagree about what will be printed: Ernie says, the value gets changed in "changeX" so it will print "7", and Bert says, no, when the function exits the changes get reversed and the value goes back to "5". Explain

  • Q : Explain the way to close an XHTML

    Explain the way to close an XHTML element.

  • Q : Types of rings in CPU Name the

    Name the different kinds of rings presented in CPU?

  • Q : State Sub type Sub type: It is a type

    Sub type: It is a type with a parent super type. The sub-type or super-type relationship is more common than the sub-class or super-class relationship. A class which implements an interface is a sub type of interface. An interface which expands the ot

  • Q : Explain While loop While loop: It is

    While loop: It is one of Java's three control structures employed for looping. The other two are: do loop and for loop. The while loop comprises of a Boolean expression and a loop body. The condition is tested prior to the loop body is entered for the

  • Q : What is an Unary operator What is an

    What is an Unary operator: It is an operator which takes a single operand. Java's unary operators are as -, +, !, !, ++ and --.

  • Q : Define Daemon thread Daemon thread :

    Daemon thread: The daemon threads are non-user threads. They are usually employed to carry out low-priority tasks which must not take priority over the major task of the program. They can be employed to do helpful work whenever all other user threads

  • Q : What is Hostname What is Hostname : It

    What is Hostname: It is the name of a host system.

©TutorsGlobe All rights reserved 2022-2023.