Well in C/C++ for example, a function declaration or prototype is just a line containing the function type, name, and the variable types it accepts.
for example:
int someFunction(int,char,float);
Then the actual function is:
int someFunction (int x, char whatever, float number){
somecode goes here;
}
Some languages require it, some don't.
2007-02-11 14:59:18
·
answer #1
·
answered by Amanda H 6
·
0⤊
0⤋
When ever you use a function in a program, the compiler has to check to make sure that you are using the function properly. For example, if the function takes an integer as an argument but you give it a double by mistake, the compiler will give an error message. So it figures that the compiler must know what the function is supposed to be like. And the compiler knows what the function is like because the programmer included a declaration to that effect. Let's say you had a function like this called foo:
int foo(float a, int b, int c)
{
.......cout << "This is an example function";
.......cout << a;
.......return (b + c);
}
This is called a function definition. It creates the function. To use the function (i.e., call the function) we would supply the 3 parameters and use an int variable to 'catch' our returned value (b+c):
float a = 12;
int b = 2;
int c = 6;
int x = 0;
x = foo(a, b, c);
So like I said, what if you made a mistake and tried to pass in an 'int' for 'a' instead of a float value like it's supposed to be? A hard to find bug would occur. So the compiler needs to know what's what so it can check our work and flag an error to notify us if we do make a mistake when we try to use the function.
The compiler always starts at the top of our program and works downward, line by line. Now if we define our function BEFORE the compiler ever sees the function call, then all is well - the compiler can use the function definition as reference to check our use of the function. However, if our function definition is located at the end of our file, or it's located in a different file or it's out in the library then the compiler will reach the function call without first seeing the definition and so it won't know about it and won't be able to check our work.
In that case, we need to provide some information on the function and place it in our code somewhere before the compiler gets to the call. Usually we place this information near the top of our file to be on the safe side. So what information do we need to supply? Well, we can just make a copy of the first line of our function definition, which is called the function header, and it is this line:
int foo(float a, int b, int c);
There, that contains all of the information needed so the compiler can tell if we are using the function properly. And this information is called a declaration (also called a function prototype). And since we got our function declaration from our function definition, it just figures that a function definition is also a function declaration. Also, in our declaration we don't really need the names of the parameters (you know, a, b and c). We can just leave them out if we want because the compiler doesn't care what we name the parameters, just what type they are. So we could just write our declaration this way:
x = foo(float, int,int);
and that tells the compiler everything it needs to know to check our work when it gets to the function call. So then we either have our function definition at the top of the program or else we use a copy of the functions header (the : x = foo(float a, int b, int c) ) to be our declaration and put that at the top of our program. But remember, a function definition is also a function declaration.
2014-09-29 22:44:28
·
answer #2
·
answered by mark_poc 6
·
0⤊
0⤋