Recently in reading a book about C, the definition of function declaration and function is very vague, not clear, Baidu a bit, found a post written is very good, reproduced over:
Original:
"Define" and "declare" a function are not the same thing. function definition refers to the establishment of function function, including specifying function name, function value type, formal parameter and its type, function body, etc., it is a complete, independent function unit. The function's declaration is the function of the name, function type and formal parameters of the type, number and order to inform the compilation system, so that when the function is called to check (for example, the function name is correct, the actual participation parameter type and number is consistent), it does not include the function body.
———— rectification, "C Program Design" (fourth edition), Tsinghua University Press, June 2010, p182
This discussion contains a number of conceptual errors that are common in many C-language books. To illustrate these errors, let's review some of the evolution and development of C language.
First, the C language code can be written like this:
main () {    printf ( } |
in the early C language, Although it is sometimes not necessary to describe the function name, there are cases where the function name is required, for example:
doub Le sqrt (); int main () {    printf ( sqrt (9.)); } |
this is because the function sqrt () The type of the return value is not an int type but a double type, and the compiler needs to know sqrt (9.) When compiling. The type of the expression.
It is not difficult to note that this description of the function name is very simple, which is the earliest form of a function type description. This description only emphasizes that the function name is a function and its return value type, and if the programmer has an error in the type or number of arguments when calling the function, the compiler is not aware of it because there is no information in the function type description in "()".
This method only shows the function name and () The result of the operation is the data type of the function return value, and it is not possible to further examine the error in the parameter.
double square ( Code class= "CPP Color1 bold" >double x) {     return x * x; } int main ( Code class= "CPP keyword bold" >void {    printf ( "%f\n"    return 0; |
This indicates that the function definition also has the effect of explaining the type of the function name, so in this sense, the function definition is also a description of the function type. This method can be used to check for errors in the number and type of arguments in a function call.
However, it is not good to use this method to illustrate the function name, because doing so in programming also needs to consider which function definition should be written in front, which is written in the following question. If function A calls function B, function B calls function C, and function C calls function A, the order in which the function definition is arranged can be confusing. In addition, this approach is not conducive to the organization of the Code, in a variety of source files composed of source programs, this writing is more stretched, flawed. Therefore, in 1990, the C standard borrowed from the C + + language to prescribe a new method of explaining the function name, which is the function prototype (functions Propotype) Type description function method:
double square (
double );
//或 double square ( double x)
int main(
void
)
{
printf
(
"%f\n" , square(3.) );
return 0;
}
double square (
double x)
{
return x * x ;
}
|
Using this approach, you can not only check the parameter type and number of errors in the function call, but also solve the problem of the organization of the source code, because the programmer no longer have to consider which function to write in front, which is written in the back of the boring problem. This approach provides a comprehensive description of the data type of the function name. It is also stated that the function definition of the formal parameter and its data type written in the "()" form belongs to the functional prototype (function Propotype) category.
Thus, the old, not the parameters of any description of the function type description, function definition and function of the prototype function type description method has the function name meaning of the utility. In this sense they are all function declarations. In the C language, the literal meaning of the word declaration (Declaration) is the significance and nature of the specified identifier (a Declaration specifies the interpretation and attributes of a set of identifier S.), the definition of an identifier is also the "declaration" (Declaration) of the marker. function definition means including the body of a function. (a definition of an identifier was a declaration for that identifier that: ... for a function, includes the function body;). A function prototype refers specifically to a function declaration that describes a parameter type, and it also contains a function definition written in this way.
Now look back at the first sentence in the sample: "The "definition" and "declaration" of a function are not the same. Since the function definition itself is a function declaration, how can you say that they are not the same thing? The logic of this sentence is like saying "man" and "man" are not the same thing. You can say that men and women are not the same, because they do not intersect. But can not say that men and people is not the same, because men are a subset of people, men is a kind of people, how to say that men and people is not the same thing?
So what should a function declaration without a function body call it? In the C language, they are called "function type Declarations" (functions types Declaration). The most important feature of a function type declaration is that it declares that the function name is a type of function and its return value, and if the type of the parameter is also declared, it is the function type declaration of the function prototype.
sample " The function's declaration is the function of the name, function type and formal parameters of the type, number and order to inform the compilation system, so that when the function is called to check (for example, the function name is correct, the actual participation of the type and number of parameters is consistent), it does not include the function body "This also does not pass." The main mistake is that it confuses the concepts of "function prototype type declaration" and "function declaration", the previous concept is only a subset of the latter concept. Function declarations contain not only "function type declarations", but also "function definitions" and old-fashioned "function type declarations". Since the function definition itself is a function declaration, it is not possible to determine whether the function's declaration includes the function body, and the old-fashioned function type declaration (for example, double sqrt ()) is also a function declaration, which does not check the parameter type and number of errors. In addition, the function declaration does not check the function name correctly or not.
"function type" in this text This concept also has errors, the function type describes not only the function return value type, but also may describe the number and type of the parameter (if it is a function prototype), and therefore cannot be compared with the "type, number of formal parameters".
The modern C language function definition and function type declaration all adopt the Function prototype style, C99 the old non-prototype form as obsolete, which means that the non-prototype form may be forbidden later.
This article was reproduced from: http://www.cnblogs.com/pmer/archive/2011/09/04/2166579.html
Analysis of "function declaration", "function prototype" and "function definition"