Definition of default function parameters

Source: Internet
Author: User

I found a small problem when writing a program today, as shown below:

 

 1 #include<iostream>
2 #include<stdlib.h>
3
4 using namespace std;
5 template<class T>
6 class Node
7 {
8 private:
9 Node<T> *next;
10 public:
11 T data;
12 Node(const T& item,Node<T>* ptrnext=NULL);
13 void InsertAfter(Node<T> *p);
14 Node<T> *DeleteAfter(void);
15 Node<T> *NextNode(void)const;
16 };
17
18 template<class T>
19 Node<T>::Node(const T& item,Node<T>* ptrnext=NULL):data(item),next(ptrnext){}
20
21 template<class T>
22 Node<T> *Node<T>::NextNode(void)const
23 {
24 return next;
25 }
26
27 int main(void)
28 {
29 return 1;
30 }

The following error occurs during compilation:

 

If the default value of the function parameter has been declared in the class definition, the function weight does not need to be declared. Therefore, the code is changed as follows:

 1 #include<iostream>
2 #include<stdlib.h>
3
4 using namespace std;
5 template<class T>
6 class Node
7 {
8 private:
9 Node<T> *next;
10 public:
11 T data;
12 Node(const T& item,Node<T>* ptrnext=NULL);
13 void InsertAfter(Node<T> *p);
14 Node<T> *DeleteAfter(void);
15 Node<T> *NextNode(void)const;
16 };
17
18 template<class T>
19 Node<T>::Node(const T& item,Node<T>* ptrnext):data(item),next(ptrnext){}
20
21 template<class T>
22 Node<T> *Node<T>::NextNode(void)const
23 {
24 return next;
25 }
26
27 int main(void)
28 {
29 return 1;
30 }

The following is a detailed explanation of this problem, from: http://blog.csdn.net/vlily/article/details/7247888

You can assign the default value to function parameters. The default value is that some parameter values can be left blank during the call. The Compiler automatically passes the default value to the call statement. The default value can be set in declaration or definition, or both can be set at declaration or definition. The default value must be the same when both are set.

Pay attention to the following points for default values:

 
1. If the default value is not set during definition, the function must be defined before the function is called. Because a wizard for this function has been provided to the compiler during declaration, compile only when the default value is set during definition
The function uses the default value only when the definition is checked. If it is defined after the call, the compiler does not know which parameter sets the default value during the call. Therefore, we usually place the default value settings in the declaration instead
Definition.
2. You cannot pass the actual value to a parameter of the reference type. You can use variables as the default value for reference type parameters. The variables must be declared and global.
When declaring a function, you must use the static member variables defined in the class or structure as the default value. If the class or structure has not yet created an instance, add the scope operator (: :) before the static member variables (::).
If an instance of the class or structure has been declared, the Instance name and member operator (.) must be added before the variable to reference its member variables as the default value of the function parameter (.).
3. if a default value is set for a parameter, the default value must be set for all the parameters in the parameter table. Otherwise, because the parameter with the default value is not listed during function calling, the compiler cannot determine whether any parameter is missing during the call.
4. during the call, if you want to replace the default value for a parameter that has already set the default value, all the parameters defined on the left of the parameter are replaced in the parameter table, whether or not the default value exists, the actual parameters must be passed.
This is also because parameters with default values are not listed during function calls. If the left side of the replaced parameter has both the default value and the default value, if the actual parameters are not passed to all parameters on the left, the compiler cannot tell which parameter the substitution value is to be passed.
For example, the following function declaration is available:
Int functionone (int x, int y = 0, int z = 0, int W = 0 );
We want to pass integer value 8 to Z for the following call:
Functionone (8 );
Obviously, the compiler cannot determine which parameter the 8 is to be passed. To achieve our goal, we must call the following code:
Functionone (0, 0, 8 );
This is where X is passed 0, Y is passed 0, z is passed 8

 

Bytes ---------------------------------------------------------------------------------------------------------------

Section 9 default parameter functions

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.