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

You may also like...

19 Responses

  1. Aankhi says:

    What is the difference between function prototype and function definition? Do we need to write the function prototype before void main()?

  2. LearnCOnline says:

    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,

  3. 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(

  4. LearnCOnline says:

    Thanks a lot santhikrishnan for the above correction… 🙂

  5. Unknown says:

    could you plz tell me wht is function prototype?

  6. Michael Cook says:

    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?

  7. Omkar Tendulkar says:

    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.

  8. 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?”

    • mk says:

      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.

  9. 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?????

  10. Anonymous says:

    float a(int,int);
    so float is compulsory?

    Sir is it compulsory to to write the name of function prototype starting with its type .

  11. how to create my own function library

  12. Vishal13 says:

    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

  13. Vishal13 says:

    Solved it?

    if

  14. Sohil Memon says:

    Thanks for the prototype 😀

  15. silki says:

    can i download all this info??

  16. Basavaraj M.H. says:

    good to share.

Leave a Reply

Your email address will not be published. Required fields are marked *

FREE C Cheatsheet - Speed Up Your C Programming.

FREE C Cheatsheet - Speed Up Your C Programming.

Download a 7-page free cheat sheet for easy and quick access to C Concepts, Snippets, and Syntax.

Thank you! Check you inbox and access your cheat-sheet