What is a Function?
A function represents set of instructions. They are expected to work similarly on various inputs given to it. It gives modularity to the program. They make development, testing as well as debugging of entire application quite easy and manageable.
There are four parts to a function:
1.Return type
2.Parameter list
3. Body of function
4. Name of function
1.Return Type: It indicates the type of data returned by function. For example, of we are returning integer than we will write int. If we are returning float then we will write float as a return type. If there is no return type then we will write void which mean returning nothing.
2.Parameter list of the function: Parameters are type and sequence of data input given to the function. For example: test(int i); , here test is name of function, int is return type and i is variable. int i inside function is parameter.
3.Body of the function: In body, we write the set of instruction that works on the input data and generate the result.
4.Name of function: It is function’s name.
Function declaration: Function declaration consists of return type, the name of function and parameter list. For example: int i=0; we can declare function as local or as global.
Function definition: In function definition we use to write actual code. Whatever, we have declared before, we have to define it in function. We can define function only once in program.
Function call: When we have to execute particular function or method we should call the function otherwise output will not execute. When we call the function it will go where the function exist and perform the output and will come back where it is called and then will continue from next statement.
Return statement: Return statement terminates the function that is currently executing.