First, check the following code. /* My_func.c -- this program uses a UDF */ # Include<Stdio. h> Void butler (void);/* ISO/ansi c function prototype */ Int main (void)/* function header */ {/* Function body start */ Printf ("I will summon the butler function. \ n "); Butler ();/* call the 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 above Code, butler appears three times. For the first timeFunction prototype; The second isFunction call statement; The third time isFunction Definition. The following describes the three appearance of butler in detail. 1.The function prototype isDeclare a functionIs a concept added to the C89 standard. The old compiler may not support function prototypes. The function prototype is used to tell the compiler that we will use a specific function, which specifies the function attribute. For example, in the butler function prototype, the first void indicates that the butler function has no return value. The second void indicates that the butler has no parameters, that is, no parameters are received. After writing the butler function prototype in front of the main function, when the main function calls butler, the compiler can detect whether the butler function call statement is correct. If the main function does not have a butler function prototype before it, we cannot call the butler function in the main function unless we put the function definition before the main function. However, putting the function definition before the main function is a bad programming style, because it will take us a lot of time to find the location of the main function. Imagine that if we have written dozens of functions, if each function definition is placed before the main function, the main function is still easy to find; however, if we accidentally put some functions behind the main function, that is, the main function is placed in the middle of a bunch of functions, it will be hard to find! C language supported before the C89 standard was introducedFunction DeclarationHowever, we can only specifyReturn Value TypeBut cannot listParameter List. For example: Void butler (); Before C89, the function was declared as described above. Both the C89 and C99 standards support this declaration method, but they also point out that this declaration method will sooner or later beElimination! Therefore, instead of using this declaration method, we should use a function prototype. 2.In the main function, the function of the statement butler (); is to call the butler function. Because the butler parameter list is empty (void), no parameters or butler (void) must be included in the brackets after the function name (butler );. 3.The butler function is defined in the same way as the main function.Function HeaderAndFunction body. The function header is almost identical to the function prototype, but a semicolon is missing. Note that a function definition is just a definition. It does not determine when a function is executed or whether the function is executed. When the main function calls the butler function, and when the butler function is executed; if the main function does not call the butler function, the butler function will not be executed. All C Programs are executed from the main function. Whether the main function is in any position, in the middle, or at the end of the source file, in short, the C program must start to execute the main function. Writing the main function at the beginning of the source file is a good programming habit, because it allows the reader to quickly figure out the program structure. So far, we have a brief understanding of the function. In the subsequent tutorials, we will learn functions more systematically. |