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 : Describe Last in-first out Last in,

    Last in, first out: It is the LIFO semantics of a stack data structure. Items are eliminated in the opposite order to which it arrived in the stack; therefore newer items are always eliminated before older ones.

  • Q : Define Heap Abstractions Heap

    Heap Abstractions: The class abstractions that we discussed above are obtained by abstracting each field of base type. The number of instances of that particular class still needs to be bounded; this results in an under-approximation that is still use

  • Q : Homework Assignment How much would it

    How much would it cost to do a basic program within the given requirements?

  • Q : State machine to identify three

    Explain a finite state machine which will detect three consecutive coins tosses of one coin which results in heads?

  • Q : Explain Connection handshake Connection

    Connection handshake: It is the exchange of messages among two processes in an attempt to create a connection between them.

  • Q : How would you extract an exact

    How would you extract an exact attribute by using XSLT, from an element into an XML document?

  • Q : Define Undeniable Signature Scheme

    Define Undeniable Signature Scheme?

  • Q : What is an Instance variable Instance

    Instance variable: It is a non-static field of a class. Each and every individual object of a class has its own copy of this field. This is in contrary to a class variable that is shared by all instances of class. Instance variables are employed to mo

  • Q : Define the term Graphical User Interface

    Graphical User Interface: A Graphical User Interface (abbreviated as GUI) is a part of a program which permits user interaction through graphical components, like menus, buttons, text areas, and so forth. Interaction frequently includes the usage of a

  • Q : Why is Java pure object oriented

    Explain the reasons that Java is pure object oriented programming language.

©TutorsGlobe All rights reserved 2022-2023.