A function is a block of code which performs a specific task and is executed individually. It is the basis of modularity that is larger programs are broken down into smaller sub-programs called functions or modules.
This makes the program easy to understand, easy to implement, and also easy to debug.
Each C program has at least one function called main () functions and it is a starting point for any program.
Each function in C has the following entities:
- Declare a function (Function prototype)
- Function Definition (Function Body)
- Function calls
Function Declaration:
It tells the compiler to expect the function in a given way that it tells the compiler the name of the function, return type, number of parameters, and sequence of parameters. This is also referred to as a function prototype.
It is performed before the main function or inside the main function or any other function.
Syntax:
returnType functionName(parameterList);
There are three important components:
Return Type:
Each function performs some sort of calculation or task and is expected to return a result and is done with the help of the keyword return.
The return type specifies the type of data/results that is data types (int, char, float, structure) of results to be returned to the program which called the function.
If the function does not need to be returned anything “void” can be used as a return type.
Function name:
This is the name of the function and should follow the same rules as that of naming a variable.
Parameters List:
This specifies the list of arguments along with the type the function expects. The caller program calls the functions with some arguments known as actual arguments and in the function, these arguments are referred to as formal arguments.
Function Defintion:
This is the actual implementation or the algorithm which does the specific task within the curly braces. It consists of three parts:
- Variables: Local variable declaration (if required)
- Instructions: A set of instructions that do the task
- Return statement: If the function returns some value and if it does not return anything (void), a return statement is not needed.
Syntax:
returnType functionName (parameterList)
{
Body of the function
...
return X;
}
Function Call:
The function call tells the compiler when to execute the function definition. When a function is called, the execution control passes to the function definition where the actual code gets executed and returns to the same caller function once the execution completes.
Syntax:
functionName(parameterList);
Types of functions:
C supports two types of functions:
- Library functions: printf(), scanf(), fegts(), getc() etc using header file.
- User defined function: User customized function for modularity of the program
Advantages of functions:
Some of the important advantages of using functions are:
- It provides modularity to the program.
- A program with functions becomes easy to maintain, implement and debug.
- Once the function is created it can be used multiple times (code re-usability).
Example:
The program below calculates the addition of two numbers using a user-defined function name “add” with two formal parameters(“num1” and “num2”).

Relevant Posts:
Categories: C Language
Leave a Reply