Functions in C
In case of solving a distinct small problems, we can write the solution program easily. But in case of solving a larger problems we write (create) a program to solve the program by dividing that larger problem into smaller sub-problems and they all are solved individually to make the program easier one by one.
In ‘C’ programming language, this concept is implemented using functions. Primary work of Function is to divide a larger program into smaller subprograms such that program becomes very easier to understand and easy to implement.
On the basis of above description a function is defined as follows…
Function is a subpart of program (or subprograms) which is used to perform specific task and is executed particularly one by one on the basis of their priority. C program can’t be assumed without a function. So functions are the backbone of ‘c’ programming language.
Every C program
must contain at least one function called main(). However a program may also
contain other functions.
Every function in C has the following
sub parts…
- Function Declaration (Function Prototype)
- Function Definition
- Function Call
1)Function Declaration
The function declaration says to the compiler about function name, datatype of the return value and their parameters.
The function declaration is also called as function prototype.
The function declaration can be performed before main function.
The function can also be declared either inside main function or inside any other function. It depends on user’s choice.
Function declaration syntax –
returnType functionName(parametersList);
In the above syntax, returnType specifies the datatype of the value which is sent as a return value through the function definiton.
The functionName is a user defined name which is used to identify the function uniquely in the program.
The parametersList is the data values which are sent to the function definition.
2)Function Definition
The function definition provides the actual code of that function. The function definition is also known as body of the function.
The actual task of the function is implemented in the function definition. That means the actual instructions have to be performed by a function are written in function definition.
The actual instructions of a function are written inside the middle braces “{ }”. The function definition is performed before main function or after main function.
Function definition syntax –
returnType
functionName(parametersList)
{
Actual code…
}
3)Function Call
The function call tells the compiler when to execute the function definition. When a function call is executed, the execution control jumps to the function definition where the actual code gets executed and returns to the same functions call once the execution completes. The function call is performed inside main function or inside any other function or inside the function itself.
Function call syntax –
functionName(parameters);
ADVANTAGES OF FUNCTIONS
- Using functions we can implement modular programming.
- Functions make the program more readable and understandable.
- Using functions the program implementation becomes easy.
- Once a function is created it can be used multiple times (the concept of code re-usability).
- Using functions even a very large program can be divided into smaller modules.