A function is a group of statements that do a specific job.
The main reason to use function is modularity and reusability.
Modularity
If you write a large program divided into small modules, you can easily maintain it. If there is an error in this program, it will be very easy to find out which module the error is in and correct it. This is why the modularity of the program. It is an important part.
Code Reusability
When you write a program, your time will be wasted because you will type the logic in three places, and the lines of your program will increase. Therefore, if you write it once as a function, you only need to give this function where you need it. .
This is the important work of code reusability.
Through this we get more benefits when we use the function.
Function Declaration:
syntax
return_datatype function_name(parameter);
Return_datatype-> function completes an operation and returns a value.
Function_name-> This name should be given as a meaningful name.
Ex:
If a function takes two values and multiplies them, it can be called multiplication so that others can understand it easily.
The value passed to the Parameters->function when it is called will be obtained through these parameters.
EX:
If the multiplication() function requires two values, it can be given as multiplication(int x , int y ). This parameter should be given inside parentheses.
function without parameter & return type:
syntax:
return_datatype function_name();
Ex:
void multiplication();
We can declare a function without taking any parameter and giving any return type.
void does not return any value.
function definition:
We will write the program inside the function definition.
Ex:
int multiplication(int x,int y)
{
int result;
result=x*y;
return result;
}