Function prototype and function definition in C++

 

        When the function declaration and definition appear after the main function, we need to add the function prototype declaration before the function (before the function call).


        The function prototype declaration indicates the name of the function, the return type, how many parameters there are, and what types of these parameters are. No function body is needed. The name of the formal parameter is also not required (of course, it is not wrong to write the name of the formal parameter). Semicolon as the end of the prototype declaration


        When the function definition appears before the main function, there is no need for the function prototype declaration.


        Function definition, there must be a function body. At the same time, there are function names, return types, and the types and names of formal parameters.


        The main difference between the two is that there is a function body, which describes the process and details of function data processing and return. The other is just to briefly talk about the function name, return type, and several parameters.


        E.g:
       

        函数原型声明:int fun(int a, int b); // 函数名字fun,返回类型 int,有2个 int 参数
        函数原型声明:int fun(int, int); // 函数名字fun,返回类型 int,有2个 int 参数
        函数原型声明:fun(int, int); // 函数名字fun,默人返回类型,有2个 int 参数
        例如函数声明和定义: int fun(int a, int b) {return a+b;}// 花括号里是函数体,结束处没有分号,有形参名。

         The function prototype is similar to the function header when the function is defined, also known as the function declaration. The function declaration consists of the function return type, function name and formal parameter list. These three elements are called the function prototype, and the function prototype describes the interface of the function.

         The function prototype is the function declaration in C++, because it defines the function name, the number of parameters, the parameter types, and the return value. The definition is the function declaration plus the realization of this function, which is followed by curly braces.

      This is the declaration (prototype):

int add(int, int);

        This is the definition:

int add(int a, int b) {
  return a + b;
}

       The function prototype is equivalent to the function call rules. For example:

函数int fun(int a,int b) { int c; c=a+b; return c }
的原型是int fun(int a,int b);或者可以写成int fun(int,int);

      This tells the caller how to call this function and what the return value is.

 

        In order to enable the function to be called before it is defined, C++ stipulates that the function prototype can be specified first, and then the function can be called. Function definitions can be placed behind the program. Since the function prototype is a statement, the function prototype must end with a semicolon. The function prototype is composed of the function return type, function name, and parameter list. It must be consistent with the return type, function name, and parameter list of the function definition. The function prototype must contain the identifier of the parameter (optional for the function declaration); Note: The system standard function is not defined in the include file, but only the function prototype is provided. When calling the function, the system will call the library function correctly. Note: The function prototype and function definition must be consistent, otherwise it will cause a connection error.

Guess you like

Origin blog.csdn.net/weixin_44684272/article/details/108304198