First, look at the following code. / * MY_FUNC.C-This program uses a custom function. #include <stdio.h> void Butler (void); / * Iso/ansi C function prototype * * int main (void)/ * Function Header /* {/ * function Body Start * * printf ("I'll summon the butler function.\n"); Butler (); / * Call Butler function * * printf ("Yes. Bring me some tea and writeable cd-roms.\n "); return 0; }/ * function body End/* void Butler (void)/ * Butler function * * { printf ("You Rang, sir?\n"); } In the code above, the butler appears 3 times. The first is as a function prototype , the second is a function call statement , and the third is a function definition . The following three occurrences of Butler are described in detail below. 1. function prototype is a way to declare a function , which is C89 The concept of standard addition, the old compiler may not support function prototypes. The function prototype is used to tell the compiler that we are going to use a particular function that indicates the properties of the function. For example, in a Butler function prototype, the first void indicates that the Butler function has no return value, and the second void indicates that Butler has no parameters, that is, no arguments are received. After the Butler function prototype is written in front of the main function, when the main function calls Butler, the compiler can detect that the Butler function call statement is correct. If there is no Butler function prototype in front of the main function, then we cannot call the Butler function in the main function unless we place the function definition before the main function. But putting the function definition before the main function is a bad programming style, because it will lead us to spend a lot of time looking for the location of the main function. Imagine if we wrote dozens of functions, and if each function definition was placed before the main function, the main function would be easy to find; but if we accidentally put some functions behind the main function, which means that the main function is placed in the middle of a bunch of functions, it's hard to look for! Before the introduction of the c89 Standard, the C language also supports the function declaration , but we can only specify the return value type of the function, and cannot list parameter list . For example: void Butler (); Before C89, functions are declared as they are written above. C89 and C99 standards support this declarative approach, but they also point out that this kind of declarative approach will be eliminated sooner or later! So let's not use this declarative approach, but use a function prototype. 2. in the main function, the statement butler (); The function is to call the Butler function. Because the argument list for Butler is empty (void), there can be no arguments in the parentheses following the function name (Butler), nor can it be butler (void);. 3. The Butler function is defined in the same way as the main function, and is composed of the function head and function body . The function header and the function prototype are almost identical, except that a semicolon is missing. Note that the function definition is just a definition, and it does not determine when the function will execute or whether the function is executed. When the main function calls the Butler function, the Butler function is executed, and if the main function does not call the Butler function, then the Butler function is not executed. All C programs are executed from the main function. Whether the main function is anywhere in the source file, in the middle or at the end, the C program must be executed from the main function. Writing the main function at the beginning of the source file is a good programming practice because it allows the reader to quickly figure out the structure of the program. So far, we've got an overview of the functions. In the following tutorials, we will learn more about functions in more detail and in a more systematic way. |