当前位置:网站首页>C language - Growth Diary -03- function definition and function prototype declaration

C language - Growth Diary -03- function definition and function prototype declaration

2022-06-11 07:42:00 Zhangxueheng

The code written like this can run normally , But if we try to remove the function declaration , What's going to happen ?

The compiler will say sum No statement , But I have stated below sum Function is written about sum The code! , Why is that ?

The reason is that the standard is different ,c The language needs to write the function in front of the main function , But doing so will affect the aesthetics of the code , So there is the function prototype declaration , Or function declarations .

such as :

  We put void sum The first line of this function is copied , Paste , Put it in main Function with a semicolon “;" In this way, we find that the code is working properly

  Simple understanding can be understood as , The functions of the functions to be used ( Not all attributes ) mention main Before the function , Equivalent to giving in advance main Function to say hello , EH , you main The function to be used in the function , I made a statement before you , Tell you in advance main Function , You can't say that the identifier used in your function body is not declared .

The function prototype : Put the function of “ head ” Take it out and add a semicolon , It forms the function prototype . The purpose of the function prototype is to tell the compiler what this function looks like , Include the name of the function 、 Parameters 、 Return type . The actual function header is main Below the function .

When the main function under the function prototype performs operations, it will also judge whether the values in the main function are consistent with those given in the function header .

The prototype of a function does not need to write the name of the parameter , namely , It is also possible to write only data types . Because what the function declaration tells the main function is that there are n A variable , this n What data types are the variables ( The data type is determined according to the problem itself ). The parameter name after the data type in the function declaration can be whether or not , But I still keep it when I write code everyday , And the following function “ head ” equally , This is because it makes sense for human readers , I can understand what it means .

原网站

版权声明
本文为[Zhangxueheng]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020519312360.html