Sunday, 15 January 2012

function in C

A function is a set of codes that performs a particular task. Once a function has been designed and packed it can be treated as a block box that take some data from the main program and return a value. It is used to divide the program in small parts.
There are two types of functions in C one is user define function and other is system define function.
There are three parts of user define function found in C:
1. Function prototype
It describes the syntax of a function it means the function is working on which type of data should be declared here. The function prototype is declared above the main function.
2. Function calling
In the function calling we pass only the variable name. It is also known as actual argument on which the function should work. It is declare within the main function.
3. Function definition
It is declared outside the main function. It works on the data which is passed by the calling function. The argument which holds the value of calling function is known as formal argument.
For systems define function the prototype and definitions are declared in the IDE or header files. So when we use any system define function, we have to use the library files.
The main advantages of making programs using functions are given bellow:
1.     The complexity of the entire program can be divided into simple subtask and then function subprograms can be written for each subtask.
2.     The subprograms are easier to write, understand, and debug.
3.     A function can be shared by other programs by compiling this function separately and loading and linking them together.
4.     In C, a function can call itself again. It is called a recursive function. Many calculations can be done easily by using recursive functions such as calculation of factorial of a number, etc.
5.     Reduction in size of program due to program code of a function can be used again and again, by calling it.
A library of a function can be designed and tested for use of every programmer.

No comments:

Post a Comment