Functions in C - Syntax, Declaration and Examples

What is a function?

A function in C is a independent or self contained block of statements that perform a specific, well defined task and may return a value to the calling program.

There are some things you should always keep in mind before writing a function. First is that you should always declare a function.

How to declare a function?

return_type function_name(type arg1, type arg2, .....);

In above declaration return_type can be any valid C data type.
function_name is the name of the function which can be anything as desired.
In bracket parameters are given. The function needs this info to perform required task. This info is passed when the function is called. The function declaration is also called as function prototype.

The above declaration gives the compiler following information:

  1. The name of the function
  2. The return_type(If not specified, by default it is integer)
  3. The type and number of arguments. (Arguments name need not to be specified just the type is mandatory.)

Syntax to write function definition

return_type function_name(type arg1, type arg2, .....)
local declarations;



The function definition can either be written before the main() function or after the main() function.

If you write it before the main function there is no need to declare that function because you have done both at the same time (the declaration and the definition).
local declarations are the declarations of variables etc which are only limited to the body of the function.

statement(s) can be any executable statement(s) which form the function body.

The return statement



return expression;


return (expression);

return statement is used to return a value to the calling program. This can appear anywhere in the function body. If this is executed the function terminates and returns a value of the data type same as the return_type of the function to the calling program.

Block Scope: The declarations which are made in the function body or within a block have block are said to have block scope. They cannot be used elsewhere outside that block.

File Scope:The declarations made outside the block are called as global declarations. They are said to have file scope because these declarations can be used anywhere in the program.

Function Call by value

This is one of the method of invoking (calling) a function. In this method values passed to the function are copied into local or temporary variables.

Example function programs in C

  1. C program to check even or odd
  2. C program to find factorial of a number using function
  3. C program to check armstrong number using fucntion
  4. C program to check give number is prime or not using a function