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 : What are the valid and invalid XHTML

    What are the valid and invalid XHTML element names?

  • Q : Explain This This : It is a Java

    This: It is a Java reserved word with numerous different uses: A) Within a constructor, it might be employed as the first statement to call the other constructor in similar class. For illustration:

    Q : Level-triggering interrupt Normal 0

    Normal 0 false false

  • Q : What is an Argument Argument : It is

    Argument: It is the information passed to a method. Arguments are as well sometimes termed as parameters. The method expecting to receive arguments should contain a formal argument declaration for each as portion of its method header. Whenever a metho

  • Q : Explain the benefits of distributed

    Explain the benefits of distributed systems.

  • Q : State the term multi threading State

    State the term multi threading.

  • Q : Create an applet of bounces in JAVA

    Create an applet that bounces a blue ball inside an applet using Thread.  The ball (diameter is 10) will start at position (0,0).  When the ball hits the edge of the applet, the ball should bounce off the edge at a randomly selected angle between 20 and 60 d

  • Q : What is an Assembly language Assembly

    Assembly language: This is a symbolic language closely analogous to the instruction set of a Central Processing Unit. The program employed to translate a program written in assembly language is termed an assembler.

  • Q : What is an Import statement Import

    Import statement: A statement which makes the names of one or more interfaces or classes accessible in a different package from the one in which they are stated. Import statements pursue any package declaration {package!declaration}, and precede any i

  • Q : What do you mean by java AWT What do

    What do you mean by the term java AWT? Describe in brief.

©TutorsGlobe All rights reserved 2022-2023.