Introduction to Function in C || Need Of Function In C Programming MSBTE
Introduction
- In day to day life a human being depend on many people to complete his task.
- For example, when a person wants to arrange function in home, lot much of work is required like, decorating the house, recruiting refreshment, inviting guests, etc.
- He hires some people and divides tasks between them The bird people know their work the person hired for decorating the house done his task properly the person hired for refreshment arrangements done his work in proper way Means that no need to tell them how to do their tasks.
- With the help of these peoples the function performed successfully.
- Similarly C language provides a term called as function which acts as hired people. The function helps to simplify the complexity of program by splitting it into smaller parts called as functions.
- C provides library functions and also allows users to define their own functions.
- Let's know why we need functions?
Need of Functions
- Every C program used main() function Sometimes the main) function is not sufficient because program may become too long and complex s the number of Instruction increases. It affects the result of debugging, testing and maintaining the program. So if the program is divided into functional parts, it will be easier to code, debug, test, and maintain the part individually and then combine into single unit. These subparts are referred functions.
- To repeat the set of statements we have used loops but In some situations set of operations needed to be performed at many different points throughout the program. For example the operation of calculating the gross salary of employee needed repeatedly but at different times in the program. One approach is repeating the statements which calculate gross salary whenever required in the program. Another approach is to construct a function for calculating gross salary at once and call it whenever required in the program.Obviously second approach saves time and space so it is better.
- A situation where multiple calculations are needed in the program and the result of one calculation may be used as input for another calculation. In this case program becomes too big and complex. The solution is to divide the program into functions as given in below Fig.
- It maintains the flow of program because data can be easily flows between functions. As functions are used to gather instructions to perform specific tasks, it reduces the confusions in different functionalities
Function
- Function is a block of code which is defined to perform specific task repeatedly. Set of functions are combined to form a program.
- Every C program has at least one function called as main() function. As the execution of a program starts from main function, it is important function.
- As we have seen earlier, a function acts as a hired person to do a specific work for us.
- Let's consider you want to do servicing of your car. So you go to the service center and say, "Service it now".As the mechanic knows his work, you don't need to tell him how to service it. You believe the car would be serviced by the mechanic in usual manner.
- Similarly a C function to calculate gross salary of employee acts like the mechanic. Whenever the operation of calculating gross salary is requiring in the program just we have to call the function.
- When a function is called, the control is transferred to the called function, the required processing is done by the function and then the control is transferred back to the main () function for further processing.
Post a Comment