简体   繁体   中英

Function declaration vs function definition

If I have this prototype:

int bar(int);

for the compiler I'm declaring the identifier bar .

If I have this definition:

int bar(int a) {};

for the compiler I'm defining the identifier bar .

Generally speaking a definition make a storage allocation for an object but:

  1. is it also true for the function declaration? Also in this case there is no storage allocated?
  2. what's the storage allocated for function definition and when it's allocated?

PS The C specification when says about declaration or definition says of their relatively of an identifier (ie the declaration of an identifier ...) But I've also read text where is said the declaration of a variable . So is there some difference or is only a "double-way" to say about one meaning?

Function declaration helps the compiler to check whether the call you make for the function obeys the same signature or not, for syntactical correctness in terms of return type, number and type of arguments and order of arguments too. A function definition is required by the linker for cross-referencing the call (use) and the definition.

When you declare a function, no storage is allocated, when you define a function, function created on relocatable reloadable address space, size depends on the machine language translation of the function, varies from platform to platform

In case of function, storage allocation is done when a function called first time. Definition itself doesn't make a storage allocation.

You should also note that, making function body empty is valid when the return type of function is void . Therefore, compiler should raise a warning against

int bar(int a) {};  

[Warning] control reaches end of non-void function [-Wreturn-type]
[Warning] ISO C does not allow extra ';' outside of a function [-pedantic]

In the C standard, every definition is also a declaration, and a function is not an object (I just mention it here, because many people use different terminologies, and I'm not sure which convention you use, I'll use the terminology of the standard, as that's common on SO).

For the standard (that is, in the abstract machine), a function takes no storage. The term “definition” is defined in C11 (n1570) 6.7 p5

A declaration specifies the interpretation and attributes of a set of identifiers. A definition of an identifier is a declaration for that identifier that: - for an object, causes storage to be reserved for that object; - for a function, includes the function body; *) - for an enumeration constant, is the (only) declaration of the identifier; - for a typedef name, is the first (or only) declaration of the identifier. [slightly different from C99, where redefining typedefs isn't allowed]

*) Function definitions have a different syntax, described in 6.9.1.

The standard uses the term “variable” only in non-normative parts like footnotes (as a noun; it's used in normative parts as an adjective, as in “variable-length array”).

For the term “identifier”, see ibid. 6.4.2.1,

(2) An identifier is a sequence of nondigit characters (including the underscore _ , the lowercase and uppercase Latin letters, and other characters) and digits, which designates one or more entities as described in 6.2.1.

and ibid. 6.2.1 p1

An identifier can denote an object; a function; a tag or a member of a structure, union, or enumeration; a typedef name; a label name; a macro name; or a macro parameter. The same identifier can denote different entities at different points in the program. A member of an enumeration is called an enumeration constant. Macro names and macro parameters are not considered further here, because prior to the semantic phase of program translation any occurrences of macro names in the source file are replaced by the preprocessing token sequences that constitute their macro definitions.

For sake of completeness, a function prototype is a declaration; old-style declarations and old-style definitions are non-prototype declarations.

What it Means to Declare Something in C and C++

When you declare a variable, a function, or even a class all you are doing is saying: there is something with this name, and it has this type. The compiler can then handle most (but not all) uses of that name without needing the full definition of that name. Declaring a value--without defining it--allows you to write code that the compiler can understand without having to put all of the details. This is particularly useful if you are working with multiple source files, and you need to use a function in multiple files. You don't want to put the body of the function in multiple files, but you do need to provide a declaration for it.

So what does a declaration look like? For example, if you write:

int func();

This is a function declaration; it does not provide the body of the function, but it does tell the compiler that it can use this function and expect that it will be defined somewhere.

What it Means to Define Something in C and C++

Defining something means providing all of the necessary information to create that thing in its entirety. Defining a function means providing a function body; defining a class means giving all of the methods of the class and the fields. Once something is defined, that also counts as declaring it; so you can often both declare and define a function, class or variable at the same time. But you don't have to.

For example, having a declaration is often good enough for the compiler. You can write code like this:

int func();

int main()
{
 int x = func();
 }

int func()
 {
 return 2;
 }

Since the compiler knows the return value of func, and the number of arguments it takes, it can compile the call to func even though it doesn't yet have the definition. In fact, the definition of the method func could go into another file!

You can also declare a class without defining it

class MyClass;

Code that needs to know the details of what is in MyClass can't work--you can't do this:

class MyClass;

MyClass an_object;

 class MyClass 
 {
 int _a_field;
 };

Because the compiler needs to know the size of the variable an_object, and it can't do that from the declaration of MyClass; it needs the definition that shows up below.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM