Function is a set of code or a block of code. Using it you can divide a large program into small segments or modules.


return-type function-name(argument)

// set of code goes here (function body)
return-type is the returning value by the function.It can be any type, i.e: int, float, double, char etc. If there is no return value then return type will be void.
function-name  is as like as variable name. When you choose a function name you should follow the rules for valid variable name.

argument  is the value provided to the function. You can provide as many value as you wish as argument.If no value is provided to a function then argument will be void.

function body is the main section of a function which contains all statements or block of code.
Explanation with example:
Suppose you are asked to write a c program to add two numbers. Your program would be:


Enter two value:10 20
Sum of 10 and 20 = 30

Now modify this above code to full fill the following condition:

  • Add two number by a user defined function
  • User defined function will not return any value
  • User defined function will not receive any value

step1: select user defined function name. Hence user defined function will add two numbers, so add  or sum is convenient name. Let we select add as our function name.

step2: Select return type.There is no return type in the condition. So return type is void.

step3: Select argument.There is no receiving data or argument in the condition. So argument will be void.

step4: write statements for function body. Your block of code will be as follows:

So your user defined function will look like this

Now all are done to add two values. But no output will be shown even you include header file as follows:

-Any C program will start execution only from the main() function and there is only one main() function in a C program and it is must. So we should write a main() function and call this user defined function from main function as follows:

So complete program will be as follows:


Enter two value:20 -15
Sum of 20 and -15 = 5


Hence itself main() is a function. So it has also return type. It is good practice to use int as return type for main() function. Because some compiler will give error message while you don’t use int as return type. And return 0; is the returning value by main() function. All function can be called by main() function but no function can call main() function due to recursive error. So it is not a meeter of concern what value is being send by main() function. It may return 0; return 200; or anything else.

If you don’t write void as argument it is okay and automatically treated argument is void. For example

But return type must be include even though it is void. For example

Next Previous