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 is Signal Handler Signal Handlers:

    Signal Handlers: In some operating systems, signal handlers are executed on a thread stack; they “overlay” the current thread execution (which may be used, for example, in combination with setjmp/longjmp to create user-domain lightweight t

  • Q : What is a Method body What is a Method

    What is a Method body: It is a body of a method: everything within the outermost block of the method.

  • Q : Define Zip file Zip file : It is a file

    Zip file: It is a file employed to store compressed versions of the files. In connection with Java bytecode files, such have mostly been superseded by the Java Archive (abbreviated as JAR) files.

  • Q : Syntax to create AJAX objects Write the

    Write the syntax to create the AJAX objects?

  • Q : What are benefits of automated testing

    What are the benefits of automated testing over manual testing?

  • 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 are good examples of XHTML

    What are the good examples of XHTML elements along with contents?

  • Q : What is Block Block : The statements

    Block: The statements and declarations are enclosed between a matching pair of curly brackets ({ and }). For example, a class body is a block, as it is a method body. The block surrounds a nested scope level.

  • Q : What is Leverage Model-Based Design

    Leverage Model-Based Design: Model-based design provides useful hints of how a large system can be reduced so that its state space becomes searchable. If not inherently visible in the design (for example, by means of using a “State” design

  • Q : CORBA allows high performance

    'CORBA was designed to allow high performance distributed applications to be written’. Discuss. Definitely, ’CORBA was designed to allow high performance distributed applications to be written’ because it provides:

©TutorsGlobe All rights reserved 2022-2023.