Parameter rules
(A) The parameter must be completely written. Do not worry about writing only the parameter type, but omit the parameter name. If the function has no parameters, use void.
(B) The parameter name should be appropriate and the order should be reasonable. For example, compile the stringcopy function, which has two parameters. Set the parameter name to strsource and strdestination. In this way, we can see that strsource should be copied to strdestination.
(C) If the parameter is a pointer and only used for input, you should add const before the type to prevent the pointer from being accidentally modified in the function body.
(D) If the input parameter is passed as a value, it is recommended to use the "const &" method to transfer the object, which can save the construction and analysis process of the temporary object, to improve efficiency.
(E) avoid having too many parameters in the function, and keep the number of parameters within five. If there are too many parameters, the parameter type or sequence may be incorrect during use.
(F) Do not use parameters with uncertain types and numbers. The C standard library function printf is a typical example of using uncertain parameters. Its prototype is:
Int printf (const chat * Format [, argument]…);
This type of function loses strict type security checks during compilation.
Return Value rules
(A) Do not omit the type of the returned value.
(B) The function name and return value type cannot conflict in semantics.
(C) The function name and return value type cannot conflict in semantics.
(D) Do not mix the normal value with the error mark to return the result. The normal value is obtained using the output parameter, and the error mark is returned using the return statement.
(E) Sometimes functions do not need to return values, but to increase flexibility, such as supporting chained expressions, return values can be appended.
(F) If the return value of a function is an object, in some cases, replacing "value transfer" with "reference transfer" can improve the efficiency. In some cases, you can only use "value transfer" instead of "reference transfer". Otherwise, an error occurs.
Internal function implementation rules
Different functions have different internal implementations, and it seems impossible to reach an agreement on internal implementations. However, based on experience, we can strictly control the "ENTRANCE" and "exit" of the function body to improve the function quality.
(A) Check the parameter validity at the "ENTRANCE" of the function body.
// Many program errors are caused by Invalid parameters. We should fully understand and correctly use assert to prevent such errors.
(B) Check the correctness and efficiency of the return Statement at the "exit" of the function body.
// If the function returns a value, the "exit" of the function is the return statement. We should not underestimate the return statement. If the return statement is not well written, the function either fails or is inefficient.
Note:
(1) The return statement cannot return "Pointer" or "Reference" pointing to "stack memory" because the function is automatically destroyed when the function body ends.
(2) Determine whether the returned result is "value", "Pointer", or "Reference ".
(3) If the return value of a function is an object, the efficiency of the return statement should be considered.
Suggestions and notes:
(1) functions should be single, rather than multi-purpose functions.
(2) The size of the function body should be small and should be controlled within 50 lines of code as far as possible.
(3) do not use the "Memory" function. The same input should generate the same output.
// The behavior of a function with the "Memory" function may be unpredictable, because its behavior may depend on a certain "memory state ". Such functions are hard to understand and are not conducive to testing and maintenance. In C/C ++, the static local variable of the function is the memory of the function. We recommend that you use less static local variables unless necessary. Check not only the validity of input parameters, but also the validity of variables entering the function body through other channels, such as global variables and file handles. the returned values for error handling must be clear, so that the user cannot easily ignore or misunderstand the error.
(4) The input parameter is the caller of the function and is passed to the real-time operator of the function. For the function, it is passed externally to the function for internal use.
(5) the output parameter is the real-time operator of the function. It is passed to the caller of the function. For the function, it is returned internally to the external function.
(6) It should be noted that some parameters are input parameters and output parameters.
(7) There can be multiple output parameters, and only one return value can be returned.
(8) output parameters are sometimes input parameters, but the returned values do not.
(9) When a function has only one output, the output parameter can be used or the return value can be used.
(10) input functions must be initialized, but input functions do not need to be initialized. However, if they are both in/out parameters, they must be initialized first.
Comparison Between Reference and pointer:
References are concepts in C ++, n is a reference of m, and m is a referent ).
Int m;
Int & n = m;
// N is equivalent to the alias (nickname) of m. Any operation on n is an operation on m. Therefore, n is neither a copy of m nor a copy of m.
In fact, n is m itself.
Some referenced rules are as follows:
(A) The reference must be initialized at the same time (the pointer can be initialized at any time ).
(B) There cannot be a NULL reference. The reference must be associated with a valid storage unit (the pointer can be NULL ).
(C) Once the reference is initialized, the reference relationship cannot be changed (the pointer can change the object at any time ).
// The main function of the reference is to pass the parameters and return values of the function. In C ++, function parameters and return values are transmitted in three ways: value transfer, pointer transfer, and reference transfer.
Sherwin, Faculty of Science, Jiangxi University of Technology