what is the difference between function declaration and signature?

前端 未结 5 1630
故里飘歌
故里飘歌 2020-12-13 00:13

In C or C++ what is the difference between function declaration and function signature?

I know something of function declaration but function signature is totally ne

5条回答
  •  离开以前
    2020-12-13 00:36

    A function declaration is a prototype. A function signature indicates what is the return type and the parameters used that makes up the signature. Consider this:

    int foo(int, int);  /* Function Declaration */
    
    
    /* Implementation of foo 
    ** Function signature
    */
    int foo(int a, int b){
    }
    

    Now, consider this scenario: a programmer is asked what is the function signature for foo:

    • It returns a datatype of int
    • Two parameters are also of datatype of int, named a and b respectively

    The function prototype on the other hand is to clue in the C/C++ compiler, on what to expect and if the signature does not match up with the prototype, the compiler will emit an error, along the context of 'function declaration error' or 'prototype mismatch'.

提交回复
热议问题