49

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 new to me. What is the point of having the concept of function signature? What are the two concepts used for actually?

Thanks!

Tim
  • 1
  • 141
  • 372
  • 590

5 Answers5

56

A function declaration is the prototype for a function (or it can come from the function definition if no prototype has been seen by the compiler at that point) - it includes the return type, the name of the function and the types of the parameters (optionally in C).

A function signature is the parts of the function declaration that the compiler uses to perform overload resolution. Since multiple functions might have the same name (ie., they're overloaded), the compiler needs a way to determine which of several possible functions with a particular name a function call should resolve to. The signature is what the compiler considers in that overload resolution. Specifically, the standard defines 'signature' as:

the information about a function that participates in overload resolution: the types of its parameters and, if the function is a class member, the cv-qualifiers (if any) on the function itself and the class in which the member function is declared.

Note that the return type is not part of the function signature. As the standard says in a footnote, "Function signatures do not include return type, because that does not participate in overload resolution".

Michael Burr
  • 333,147
  • 50
  • 533
  • 760
  • 3
    It is not exactly correct to say that "function declaration is the prototype". In C++ it is practically correct, but terminologically wrong, since in C++ there's no such term as "prototype". In C it is strictly incorrect because in C function declaration is not necessarily a prototype. For example, `void foo();` is a function declaration that does not introduce a prototype in C. Actually, this question is not supposed to be tagged C, since there's no notion of "function signature" in C. It is purely a C++ question and, for this reason, the answer should not mention any "prototypes". – AnT stands with Russia Mar 08 '10 at 16:33
  • 4
    Could you please cite sources? – Manuel Arwed Schmidt Nov 03 '15 at 16:03
  • 2
    You introduced "signature," "declaration," "prototype," and "definition" without clear distinctions. Could you edit this to add a concrete example of each? – Andy Ray Jul 01 '21 at 20:44
7

The standard defines two terms: declaration and definition. A definition is a tentative declaration. However, the C99 and C++03 standards have slightly varying definitions.

From C++0x draft:

Appendix C

8.3.5 Change: In C++, a function declared with an empty parameter list takes no arguments. In C, an empty parameter list means that the number and type of the function arguments are unknown"

Definitions

1.3.11 signature

the name and the parameter-type-list (8.3.5) of a function, as well as the class, concept, concept map, or namespace of which it is a member. If a function or function template is a class member its signature additionally includes the cv-qualifiers (if any) and the ref-qualifier (if any) on the function or function template itself. The signature of a constrained member (9.2) includes its template requirements. The signature of a function template additionally includes its return type, its template parameter list, and its template requirements (if any). The signature of a function template specialization includes the signature of the template of which it is a specialization and its template arguments (whether explicitly specified or deduced). [ Note: Signatures are used as a basis for name mangling and linking.—end note ]

Community
  • 1
  • 1
dirkgently
  • 108,024
  • 16
  • 131
  • 187
  • 2
    Thanks but I feel difficult to understand what you are trying to say. – Tim Feb 24 '10 at 00:16
  • I know of function declaration but function signature is totally new to me. What is the point of having the concept of function signature? What are the two concepts used for actually? – Tim Feb 24 '10 at 00:22
  • 3
    @Tim: 1) The C standard does not define the term signature. It is used in general programming literature loosely and is often synonymous with a function declaration. Note that function declarations need not have parameters. Definitions do, though, if you are using them in the definition. 2) Signatures are widely used by the compiler to e.g. provide a definition for an identifier and in case of C++ choose the best overload. – dirkgently Feb 24 '10 at 00:26
5

The function signature doesn't include the return type or linkage type of the function.

OK, Wikipedia disagrees with me on the return type being included. However I know that the return type is not used by the compiler when deciding if a function call matches the signature. This previous StackOverflow question appears to agree: Is the return type part of the function signature?

Community
  • 1
  • 1
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • Does "storage class" mean the class which the function belongs to? What is the point of having the concept of function signature? Thanks! – Tim Feb 24 '10 at 00:10
  • @Tim: No. They refer to the linkage applicable. – dirkgently Feb 24 '10 at 00:15
  • I wouldn't put too much credence in Wikipedia's article right now. It's not well written. The *Method signature* article is a little better. – Rob Kennedy Feb 24 '10 at 00:16
  • I suspect I'm confusing my terminology - storage class refers to variables, not functions. What I meant is the characteristics implied by the static or extern keywords for example. I've updated my answer. – Mark Ransom Feb 24 '10 at 00:16
  • @Mark Ransom: `static` and `extern` are storage classes that *can* apply to functions in both C and C++. – dirkgently Feb 24 '10 at 00:24
  • In C++ the return type is not part of the function signature, regardless of what Wikipedia says, though return type might be part of the function signature for other languages (I honestly don't know). – Michael Burr Feb 24 '10 at 01:11
3

Also please note that top-level const and volatile on argument are not part of the signature, according to the standard. But some compilers get this wrong.

e.g.

void f(const int, const char* const);

has the same signature as

void f(int, const char*);
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
-1

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'.

t0mm13b
  • 34,087
  • 8
  • 78
  • 110