Quote:
Originally Posted by Nylex
Function prototypes don't make code "behave differently". The point of them is to provide a definition of the function before use. The compiler will usually complain (probably not an error, but a warning) if you've used a function that hasn't been previously declared. See this.
|
Slight correction above. The function prototyping means "declarations" and not "definitions"
a function definition is when we program the function, that is write the code of the function.
A declaration is to notify the compiler that a function of the name with the parameters will be defined elsewhere in the program. Like
int my_function(int a,char b,float c);
or
int my_function(int ,char , float);
This is a function declaration (local or global depending on the location)
The above statements lets the compiler know that the function named "my_function" with the return type "int" and first argument "int" second argument "char" and third argument "float".
After we declare this the compiler knows the function identifier "my_function" and does not generate any error when it encounters it in the program in that function call. When we write the function ie define it elsewhere, the compiler gets it and checks that if the definition types and the declaration types match, if yes then okay , else it gives an error message.
If you pass an int in place of the float parameter that is pass some other parameters to the function when calling than it was described in the declaration then it gets you a warning of incomparable argument passing or similar.
Example
main()
{
int integer;
float floating_point;
char character_type;
....
...
my_function(floating_point,integer,character_type);
.
}
The above example passes float type data in place of integer and simmilar mixup is done.
Then the compiler will warn you.
So prototype gets you strong type checkups during compilation, so you can do a more organized programming, with lesser errors.
This need if felt when a lot of functions with many arguments are used and you need to keep care of all the parameters are passing of the correct type. Also in a multifile program each function cn stay in a single file. When compiling you need to write the protypes to let the compiler know the function names and its types to compile the program a file at once. This can be done with creating a .h file (header file) with all the prototypes and #including them. (Later linking the objects make a single executable)
In absence of the declaration the typechecks cannot be done, and you might accidentally pass an floating point value as a function argument where it actually was programmed to accept a pointer. Then your program will crash in runtime, and if the program is large you can overlook that small fault.
Prototyping also gets a list of functions used in a program. so if you are using a lot user defined function in a program then you have the full of functions which comes handy while programming and also developing. (personal view)
Please correct me if there is any wrong information.
Please add some more points of prototyping.
and Please ask if anything is still unclear.