Function declaration and Prototype in C Programming Language
By default, any C function returns an int value. If we want that the function should return a value other than int then we need to explicitly mention it in the function definition. Let us understand this using an example.
Example 1:
float fnGetAverage(int, int); //function prototype void main() { int x,y; float z; printf(“\nEnter the values for x and y\n”); scanf(“%d %d”, &x, &y); z = fnGetAverage(x,y); //function call printf(“\nThe average is %f”, z); } float fnGetAverage(int a, int b) { //function definition float c; c = (a+b)/2; //calculating average return c; //returning the value }
Output:
Enter the values for x and y 3 2 The average is 2.500000
What is the difference between function prototype and function definition? Do we need to write the function prototype before void main()?
Function definition is writing the code within the function.
If we want to call the function before defining it then we need to prototype it. Function prototyping is to tell the compiler that we are using the function name “some function” before defining it. It is defined somewhere later in the program.
Function protype is written before the main().
It is necessary to do function prototyping if we are calling it before the function definition.
Hope this clarifies.
Thanks,
Hi Buddy
This is a nice website for beginners. And I learned alot from here. I think there is a small mistake in this example program.
“” printf(
Thanks a lot santhikrishnan for the above correction… 🙂
could you plz tell me wht is function prototype?
When I tried the above, if the answer ended with .5, it would round the value down.
So where you suggest to enter 2 & 3 for x & y, the answer I got was 2.000000.
However, if I change c = (a+b)/2 to c = (a+b)*0.5 is gives the correct answer 2.500000
Can you please explain?
If we cannot use a prototype declaration in function,then what will happen?In case some compilers don’t allow to use it while doing a program on function.
I have this also
“When I tried the above, if the answer ended with .5, it would round the value down.
So where you suggest to enter 2 & 3 for x & y, the answer I got was 2.000000.
However, if I change c = (a+b)/2 to c = (a+b)*0.5 is gives the correct answer 2.500000
Can you please explain?”
Can you please explain?”
I realize this is an old comment but for others with this problem…. it must be entered as 2.0 instead of just (2). Otherwise it treats it as integer division which drops everything to the right of the decimal. Try a=2/3 and when you printf a it will be 0. At least one of the operands has to have a decimal.
I also found that any result (set as float type) of a calculation that only includes integers will truncate to an integer although it still displays the decimal places. Using /2.0 (a float value instead of integer 2) also gives the correct answer. 2.500000.
Compiler?????
float a(int,int);
so float is compulsory?
Sir is it compulsory to to write the name of function prototype starting with its type .
how to create my own function library
Solved it?
if “%f”
then
c=(a+b)/2;
average=3.00000
AND
if “%f”
then
c=(a+b)*0.5;
average=3.50000
AND
if “%f”
then
c=(a+b)/2.0;
average=3.50000
Please solved this problem because i am confused
Solved it?
if
Thanks for the prototype 😀
You are welcome. 🙂
If you liked it then kindly share it.
Thanks,
LearnCOnline Team
Of course, I will do Sir. Surely, my friends will also like this site because you are sharing all the needed information for the programmers.
can i download all this info??
good to share.