Default parameter functions

Source: Internet
Author: User

1. Purpose of default parameters

C ++ can define default parameter values for functions. Generally, when calling a function, you must specify the corresponding real parameters for each parameter of the function. For example:
Void delay (INT loops); // function declaration
Void delay (INT loops) // Function Definition
{
If (100 ps = 0)
Return;
For (INT I = 0; I <loops, I ++ );
}
Whenever you call the delay () function, you must pass a value to loops to determine the time. But sometimes the delay () function needs to be called repeatedly with the same real parameters. C ++ can define default values for parameters. If you define loops in the delay () function as the default value of 1000, you just need to change the function declaration:
Void delay (INT loops = 1000 );
In this way, no value is assigned to the loops whenever the delay () function is called, and the program will automatically process the value as 1000. For example, call:
Delay (2500); // set loops to 2500
Delay (); // OK: the default value of loops is 1000.
In the call, if no parameter is provided, work is performed according to the specified default value.
The default parameter value of a function is allowed to make programming simple and enable the compiler to perform more checks and errors.

2. Declaration of default parameters

The default parameters are provided in the function declaration. When there are declarations and definitions, the default parameters are not allowed in the definition. If a function is defined only, the default parameter can be displayed in the function definition. For example:
Void point (Int = 3, Int = 4); // The default value is given in the Declaration.
Void point (intx, inty) // The default value is not allowed in the definition.
{
Cout <x <Endl;
Cout <Y <Endl;
}

3. Sequence of default parameters

If a function has multiple default parameters, the default parameters in the parameter distribution should be defined from right to left. When a function is called, parameters can only be matched to the left. For example:
Void func (int A = 1, int B, int c = 3, int d = 4); // Error
Void func (int A, int B = 2, int c = 3, int d = 4); // OK
For the 2nd function declarations, the calling method is as follows:

Func (10, 15, 20, 30); // OK: All real parameters are provided during the call.
Func (); // error: parameter A has no default value.
Func (I2, 12); // OK: default values of parameters C and D
Func (, 20); // error: the default value can only be matched from right to left.

4. default parameters and function Overloading

By default, you can combine a series of simple overload functions into one. For example, the following three overload functions:
Void point (INT, INT ){//...}
Void point (int A) {return point (A, 4 );}
Void point () {return point (3, 4 );}
The following default parameter functions can be used as an alternative:
Void point (Int = 3, Int = 4 );
When "Point ();" is called, that is, "point (3, 4);" is called, which is 3rd declared overload functions.
When "Point (6);" is called, that is, "Point (6, 4);" is called, which is 2nd declared overload functions.
When "Point (1st);" is called, declared overload functions are called.
If a group of overload functions (which may contain default parameters) allow calls with the same number of instances, this will lead to the ambiguity of calls. For example:
Void func (INT); // One of the overloaded functions
Void func (INT, Int = 4); // reload function 2 with default parameters
Void func (Int = 3, Int = 4); // reload function 3 with default parameters

Func (7); // error: Which of the three overload functions is called?
Func () // error: Which of the following two overload functions is called?

5. Limits on the default value

The default value can be a global variable, a global constant, or even a function. For example:
Int A = 1;
Int fun (INT );
Int g (int x; fun (a); // OK: allows the default value to be a function.
The default value cannot be a local variable, because the function call of the default parameter is determined during compilation, and the location and value of the local variable cannot be determined during compilation. For example:
Void fun ()
{
Int I;
Void g (INT x = I); // error: I is invisible when processing g () function declaration
}

Summary of this Chapter

As the program volume and complexity increase, the best way is to divide the program into smaller modules that are easier to manage. Such modules are functions.
The function name is recommended to reflect the task to be completed.
A function can return data to the caller. to return a value, you must specify the type of the returned value before the function name. If the function does not return a value, the type is void.
The program passes information to the function through parameters. If the function needs to accept parameters, it must specify the name and type for the parameters.
C ++ must know the return type of the function and the number and type of accepted parameters. If the function definition appears after the function is called, it must be declared using the function prototype at the beginning of the program.
Local variables are defined within the function and can only be accessed by the function that defines the variable. A global variable is a variable that runs throughout the program. Define global variables at the beginning of the program and place them outside all functions.
Static local variables are defined within the function, but the life cycle is generated when the function is called for the first time and ends with the end of the program, static local variables can only be seen in the function that defines the variable.
The function call mechanism is implemented by the stack operation process. A function can be called recursively. Function definitions cannot be placed in any function definition.
Inline functions are implemented to improve programming efficiency. they overcome the disadvantages caused by # define macro definition.
Function overloading allows you to define multiple functions with the same function name. The Connection Program calls the corresponding function based on the number, type, and sequence of parameters passed to the function. Function overloading simplifies program design. programmers can complete a series of related tasks by remembering a function name.
You can specify the default parameter value through the value assignment operation in the function definition. Once the program defaults the parameter value when calling the function, the function uses the default parameter value. The default value cannot be used between parameters. Specifying the default parameter value can simplify the use of functions and enhance the reusability of functions.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.