Function
Definition:
A set of statements to perform a specific task is understood as Function.Functions are of two types:
1. Predefined Functions (Or) Library Functions
2. User-Defined Functions
1. Predefined Functions:
A function that’s already defined by the system is understood as a Predefined Function or Library Function
Ex: clrscr(), getch(), sqrt(),printf(),scanf()etc.,
2. User-defined Functions:
A function that’s defined by the user is understood as a User-defined Function.
Ex: sum(), apple(), mango(), raju(),etc.,
Uses of functions:
1. If we would like to run a task repetitively then it is not required to rewrite the actual block of the program over and once again. Shift the actual block of statements during a user-defined function. The Function defined is often used any number of times to perform the task.
2. Using functions large programs are often scaled right down to smaller ones. It’s easy to debug and find errors within it. It also increases readability.
How does Function work?
1) Once a function is defined and called, it takes some data from the calling function and returns a worth to the called function.
2) The detail of the inner working of a function is unknown to the remainder of the program. Whenever a function is named, control passes to the called functions and therefore the working of the calling function is stopped. Once the execution of the called function is complete, the control returns to the calling function and carries out the subsequent instructions.
3) The value of the particular arguments skilled the calling function is received via the formal arguments of the called function. the amount of real and formal arguments should not differ. Extra arguments are discarded if they are defined. If the formal arguments are more than the particular arguments then the additional arguments appear as garbage. Failure to match the data type will end in an unintended result.
4) The function operates on formal arguments and sends back the result to the calling function. The return( ) statements performs this task.
