C function advantages
Contents
- 1 Advantages of Writing Function in C Programming
- 1.1 1. Modular and Structural Programming can be done
- 1.2 2. It follows Top-Down Execution approach , So main can be kept very small.
- 1.3 3. Individual functions can be easily built,tested
- 1.4 4. Program development become easy
- 1.5 5. Frequently used functions can be put together in the customized library
- 1.6 6. A function can call other functions & also itself
- 1.7 7. It is easier to understand the Program topic
Advantages of Writing Function in C Programming
1. Modular and Structural Programming can be done
- We can divide c program in smaller modules.
- We can call module whenever require. e.g suppose we have written calculator program then we can write 4 modules (i.e add,sub,multiply,divide)
- Modular programming makes C program more readable.
- Modules once created , can be re-used in other programs.
2. It follows Top-Down Execution approach , So main can be kept very small.
- Every C program starts from main function.
- Every function is called directly or indirectly through main.[See : How to call a function (Simple Example) ]
- Example : Top down approach. (functions are executed from top to bottom)
main() { display(); } void mumbai() { printf("In mumbai"); } void pune() { india(); } void display() { pune(); } void india() { mumbai(); }
Now in the above program we have written functions in the following order -
- main()
- mumbai()
- pune()
- display()
- india()
We have written functions in the above specified sequence , however functions are called in which order we call them. Here functions are called this sequence -
- main()
- display()
- pune()
- india()
- mumbai().
3. Individual functions can be easily built,tested
- As we have developed C application in modules we can test each and every module.
- Unit testing is possible.
- Writing code in function will enhance application development process.
4. Program development become easy
5. Frequently used functions can be put together in the customized library
- We can put frequently used functions in our custom header file. [ See : How to create own header file ? ]
- After creating header file we can re use header file. We can include header file in other program.
6. A function can call other functions & also itself
- Function can call other function.
- Function can call itself , which is called as “recursive” function.
- Recursive functions are also useful in order to write system functions.
7. It is easier to understand the Program topic
- We can get overall idea of the project just by reviewing function names.