Functions in C

Let us understand the concept of functions in C language with the help of day to day scenario.

Let us suppose that we want to write a program that will perform all the operations required for banking. So we will have to write a program that will have following steps:

  1. Accept account number and other personal details of the customer as a input
  2. Accept the details of the transactions to be performed
  3. If the customer wants to withdraw the money, then program should fetch the balance amount and perform eligibility check
  4. After performing eligibility check, the money is deducted from that person’s account and the details of the transaction are updated in the database

If we write the code for the above mentioned steps, it becomes very lengthy code. The solution to get rid of such lengthy code is the use of functions.

What are C functions?

Functions are self contained block of statements that perform a particular task.

For the above example we can write following functions in C:

  1. Function to accept the personal details from the customer
  2. Function to verify the details
  3. Function to withdraw the amount

Thus, the C functions increases the readability of the program. Also, in case if we need to modify particular part of the program it becomes very easy to identify that part and modify it.

Example 1:

void main()

void fnCallingFunction1()
	printf(“You are in fnCallingFunction1”);

You are in fnCallingFunction1

As we all know, the execution of the program always starts from the main() function.

Inside the main() function there is a function call to function fnCallingFunction1(). When the statement fnCallingFunction1() is encountered, the control is transferred to the function fnCallingFunction1 written just after the main() function. Now, all the statements written inside the fnCallingFunction1() is executed. There is a printf statement inside the body of fnCallingFunction1(). Hence the output “You are in fnCallingFunction1”.

Note: A function in C cannot be defined inside the body of another function.


float fnGetAverage(int, int); //function prototype

void main()
    int x,y;
    float z;
    printf(“\nEnter the values for x and y”);
    scanf(“%d %d”, &x, &y);
    z = fnGetAverage(x,y); //function call
    printf(“\nThe average is %d”, z);

float fnGetAverage(int a, int b)
{ //function definition
    float c;
    c = (a+b)/2; //calculating average
    return c; //returning the value

Here, the value of x and y is taken as input from the user. This value is then passed to the function fnGetAverage as shown below:


Now, the control is transferred to the function definition. The value of x and y are taken in the variables a and b respectively. The average of a and b is performed in the function body and the result is stored in the variable c. This value is then returned back using the statement return c. Now the control is transferred to the main() function and the value is assigned to the variable z.

The variables x and y are called ‘actual arguments/actual parameters’ and the variables a and b are called ‘formal arguments/formal parameters’. We can pass n numbers of arguments. But the type, order and the number of arguments must be same.

In the above program, the value of x and y is not known to the function “fnGetAverage”.

When return statement is encountered the control is transferred back to the calling program.
It returns the value present in the parentheses or the variable.

return 8;
return (8);

Also see:
How does using functions in C make writing large programs easier?
Coding Standards for Writing Functions in C

You may also like...

27 Responses

  1. Anonymous says:

    How can a function return a constant value? If a constant value needs to be returned why to write a function??

  2. LearnCOnline says:

    Function can return a constant value as shown in the example.
    Sometime, we need to return a constant. In the C Programming we might return a constant 0 or 1 to indicate the successful execution of the program. If there is some kind of error we might return constant ‘0’ to the calling program to indicate there was some kind of error. Similarly there are various types of error and we can use ‘n’ number of constants to indicate the type of error.
    This is just an example. we might want to return constant value based on the logic used for programming.

  3. Anonymous says:

    In formal parameters can i take x and y instead of a and b.

  4. LearnCOnline says:

    Yes you can use x and y instead of a and b.

  5. Anonymous says:

    what is the need to use a function call,we can simply calculate the average in the main program itself,then what is the need of function call?

  6. Anonymous says:

    which is calling function and which is called function in programme?

  7. Anonymous says:


  8. parth jani says:

    is it possible to return more than one value from function ??? if yes then how ???

  9. kevin kettle says:

    Yes is it possible to return two variables/values from a function??

  10. LearnCOnline says:

    No… its not possible to return more than one value from a function

  11. parth jani says:

    how is it possible ?? if yes….

  12. Anonymous says:

    this example is too hard i can’t undrestand pls. do easy like normal function declaration

  13. Anonymous says:

    couldn`t understand this page!

  14. Anonymous says:

    Unable to understand :/ plz make this easier

  15. Sinya says:

    i don understand when to write the printf and scanf.WhY IN THE ABOVE PROGRAM THEY WRITTEN ONE AFTER ANOTHER?

  16. Marie Curie says:

    when we run fnGetAverage() for x=2 and y=3, we get 2.000000. But why don’t we obtain 2.500000 (i mean the exact value) if we work with floats?

    • Nikola Tesla says:

      here x and y or the parameters passed in the function are declared as ”int” hence the result is will also in “int”[ (a+b)/2 ,getting ‘2’ only as they are int] after that you are assigning the value ‘2’ to ‘c’, which is float. so when you print ‘c’ it shows in float 2.000000.
      If you want 2.500000. you should declare any one of the value (either x or y) as float.

  17. Anonymous says:

    this is very helpful to new c learners!!!!!

  18. Anonymous says:

    very helpful, everything explained very clearly. could you please explain the reason behind including the ‘function prototype’ . thank you

  19. LearnCOnline says:

    Please refer to the below link for more information:

    Alternatively, you can refer to the below link as well:

    LearnCOnline Team

  20. Christopher says:

    @Marie Curie – great question. It seems that we need to ‘cast’ the integer result of the addition of (a + b) to be a float, before doing the division.
    Like this: c = ((float)(a + b)) / 2;
    You should then get 2.500000 when you are using “2 and 3”.

    • ANSHIKA SAXENA says:

      its necessary to take function ,argument & return datatypes as same, the problem seems to be solved. i.e if we take float instead of int in functn prototype
      =>fnGetAverage(int, int); //function prototype

      void main()
      float x,y,z;


  21. Trixico Qais says:

    program is not working properly taking input from user but there is no output for average is showing .

  22. Im using Dev c++ and when i use void main() i get this erro:
    [Error] ‘::main’ must return ‘int’

    but when i change it to int i get zero for the average please help

  23. Anonymous says:


  24. WAP for swapping of two arrays using a function

  25. ganesh says:

    exactly why we should use function?? i am not getting will you please explain ??
    Instead of calling function n all why we cant directly define our operation in main block itself??

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