A function is a group of statements those perform a specific task. Every function in C has following components -
returnType functionName(parameters list )
returnType: A function may or may not return a value. returnType is the data type of the value the function returns. When function is not returning anything the returnType can be void or may not be specified.
functionName: Name of the function, along with the returnType and Parameter it constitute the function signature.
Parameter List: A parameter list a the variable list which is passed from the caller to the called function.
Function Body: A set of statements about the task of the function.
Advantages of Functions
- We can divide c program in smaller modules which can be tested and reused in the system as per need.
- Function increases modularity of a program which makes the code more readable.
- Program development become easy because of function i.e. top-down approach.
- Frequently used functions can be put together in the customized library
- A function can call other functions & also itself and can reduce the programming effort.
Arguments to the function
A function must declare variables that accept the values passed from the caller function. These variables are called the formal parameters of the function. These are like other local variables inside the function and are created upon entry into the function and destroyed upon exit.
There are two types of arguments can be passed to a function:
Call by value: This method copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument.
Call by reference: This method copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. This means that changes made to the parameter affect the argument.