What are they used for and why are they necessary?
What are they used for and why are they necessary?
They are used so that the compiler 'knows what is coming', in a manner of speaking. When you compile something the compiler goes through your code line by line and if you have a reference to a function which it isn't familiar with then you will have problems.
E.g. This will cause problems without a prototype.
But this won't have any problems without a prototype.Code:/* Includes */ int main() { RandomFunction(); return 0; } int RandomFunction() { /* Do something */ return 1; }
Code:/* Includes */ int RandomFunction() { /* Do Something */ return 1; } int main() { RandomFunction(); return 0; }
Thank you sir. So basically if you use prototypes then it won't matter what order you list your functions? Any other good reasons they are used?
I found them useful during a group assignment to help divide up the tasks, a function prototype along with a description of what the function should do is typically all that is needed
╔╗╔══╦╗
║║║╔╗║║
║╚╣╚╝║╚╗
╚═╩══╩═╝
Sometimes they are necessary. E.g. if you have functions A and B, and function A calls function B and function B calls function A, then you are going to have problems without them.
e.g.
Bit of a useless piece of code, but it shows the principle. Without function prototypes, how do you get the code to execute? This can become quite important when you get onto larger projects and you have classes (C++, not C) which have to interact via pointers to each other. I've needed to use prototypes to provide forward declaration of classes which are only properly defined later in the source code.Code:int FunctionA(int val) { /* Do something with val - example only */ int val2 = val -5; FunctionB(val2); } int FunctionB(int val) { int val2 = val + 3; FunctionA(val2); }
Edit:
That is very true. Helps document exactly what is going on. Very useful for bigger projects.I found them useful during a group assignment to help divide up the tasks, a function prototype along with a description of what the function should do is typically all that is needed
For larger projects where there are multiple c source modules, function prototypes are placed in .h files and accessed using the #include directive in each .c module. That way, each module is assured they are using the same consistent definitions.
Prototypes are similar in concept to the purpose of "extern" variable declarations. They specify the data type so that the compiler can go ahead without getting stuck on missing definitions.
Hinted at by nonoob, the purpose of a function prototype is so the compiler can do a proper job of type checking arguments and return values being passed around and then whine at you when you attempt to pass a float* where a char is expected.
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens