C function declaration, function prototype, function definition __ function

Source: Internet
Author: User
Tags define definition function definition function prototype
development of function declarations 1 implicit function Declaration implicit function declarationThe main function and the printf function return value type int, if not declared, the compiler default function return value is the INT type C90 standard, this kind of writing enters the process which is gradually discarded (although it was not abolished completely immediately) C99 abolished the implicit function declaration law (remove impli CIT function declaration), omitting the int in front of main () is no longer allowed
Main ()
{
    printf ("Hello, world!\n");
}
2 function type declaration function type declarationThe SQRT function type is not int and requires a function type description (declaring function types and functions names only) when calling a function there is an error in the type or number of arguments the compiler is not aware
Double sqrt ();

int main ()
{
    printf ("%f\n", sqrt (9.));
3 function Definition declarationWrite function definition before function call, this kind of declaration can check out the error in number and type of parameter when function call is in the source program composed of multiple source files, the organization function definition declaration that is unfavorable to code is the function prototype declaration
Double square (double x)
{return
    x * x;
}

int main (void)
{
    printf ("%f\n", Square (3));
    return 0;
}
4 function Prototype declaration function Propotype

C Standard for reference to C + + language specified function prototype (functions Propotype) Declaration, not only can check the function call when the parameter type and number of errors, and solve the source code organization problem

Double square (double x);

int main (void)
{
    printf ("%f\n", Square (3));
    return 0;
}

Double square (double x)
{return
    x * x;
}
Summary

function type declarations, function definitions, function prototypes are function declaration Declarations Declaration

Specify the meaning and nature of identifiers

A declaration specifies the interpretation and attributes of a set of identifiers. Define Definition

The definition of an identifier (definition) is also the declaration of this identifier (declaration), and the function definition includes the function body

A definition of an ' identifier is ' a declaration for that identifier that: ... for a function, includes the function body prototype Propotype

A function declaration that includes a description of the parameter type, which also contains a function definition error that is written in this way.

Tan Haoqiang, "C Programming" (fourth edition), Tsinghua University Press, June 2010, p182

The definition of function refers to the establishment of function function, including specifying function name, function value type, formal parameter and its type, function body, etc., which is a complete and independent function unit.

The function's declaration function is to put the function's name, the function type and the type, number, and order of the formal parameters inform the compilation system so that the function is not included when it is invoked (for example, if the function name is correct, the type and number of the actual participating parameters are the same). Analysis

"The definition and declaration of a function is not the same thing" is wrong, the function definition itself is a function declaration, is a subset relationship.

The function's declaration is to notify the system of the name of the function, the type of function, and the type, number, and order of the parameters, so that when the function is invoked, it does not include the function body. The function definition itself is a function declaration, so it is not possible to determine whether the declaration of a function includes a function body, and the old-fashioned function type declaration belongs to the function declaration, which does not check for errors in the type and number of arguments. In addition, the function declaration does not check whether the function name is correct or not. The concept of "function type" in this text also has errors, and the function type describes not only the function return value type, but also the number and type of the parameter (if it is a function prototype), so it cannot be compared with the "type, number" of the formal parameter. function Declaration functions The called function returns the function return value to the stack or register according to the function type, and the calling function accesses the memory or register position according to the type provided by the function prototype C + + p204 Call function primer The parameter type provided by the function prototype. Pass in an argument that determines the type and quantity, so that the modulated function can correctly handle incoming parameter references

Analysis of "function declaration", "function prototype" and "function definition"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.