Unprotected feature declarations

How is function declaration different from function prototype?

I ask this question in connection with this answer to the question.

+4
source share
4 answers

A function declaration may or may not include function arguments.
Whereas the Function Protocol must contain the arguments of the function.

From Wikipedia :
Consider the following function prototype:

int fac(int n); 

This prototype indicates that this program has a function called fac that takes a single integer argument n and returns an integer. Elsewhere in the program, a function definition must be provided if you want to use this function.

It is important to know that a function declaration does not have to include a prototype. The following is a description of a function without a prototype, which simply declares the function name and its return type, but does not indicate what types of parameters are required to be defined.

 int fac(); 
+3
source

A prototype is an announcement, but a declaration is not always a prototype. If you do not specify parameters, then this is only a declaration, not a prototype. This means that the compiler will not reject the call to this function, complaining that it has not been declared, but will not be able to verify the correctness of the parameters passed (as if you had a prototype).

+3
source

A function prototype is a function declaration that defines the number and types of parameters.

 T foo(); // non-prototype declaration T foo(int, char *); // prototype declaration T foo(int a, char *b); // prototype declaration 
0
source

The prototype tells the compiler that there is a function that looks like this, and that is its name int getanint() . When you use this function, the compiler places code calls that function, and leaves room for the address to be inserted into the code that defines what this function does.

So, in the file header A;

 int getanint(); 

IN main.c

 int main(...) { getanint(); } 

when you compile main.c, it does not know what getanint does or even where it is created .o file is incomplete and not enough to create the actual program. remember that the compiler works with one file, this file can be very large due to the #include directives, but they create one file.

When you execute A.cpp

 int getanint() { return 4; } 

You now have the code for getanint in the object file.

To actually create a program, you must get main.o and Ao together and have function definitions inserted in the appropriate places. This is the task of the linker.

0
source

Source: https://habr.com/ru/post/1393379/


All Articles