Now lets continue from where we left off our topic “function “. In this post we are going to discuss about types of functions .
TYPES OF FUNCTIONS
Now there are 4 types by which we can make a function :
- function with return type and no parameter
- function with no return type and no parameter
- function with no return type and parameter
- function with return type and parameter
FUNCTION WITH NO RETURN TYPE AND NO PARAMETER
SYNTAX
void function_name()
{
statements ;
}
Now clearly no return types means “void ” datatype and no parameter means there wont be any parameter . Now consider this example it will help you to understand better .
EXAMPLE
FUNCTION WITH NO RETURN TYPE AND PARAMETER
SYNTAX
void function_name( parameter , parameter,..)
{
statements ;
}
Now clearly no return types means “void ” datatype but parameter is taken means there will be variables either entereed by the user or by programmer as discussed earlier . Now consider this example it will help you to understand better .
EXAMPLE
here in sum(a,b) we are passing the variables to the function defined .
FUNCTION WITH RETURN TYPE AND NO PARAMETER
SYNTAX
datatype function_name()
{
statements ;
return variable;
}
Now return types means any datatype other than void and no parameter means there wont be any parameter . Now consider this example it will help you to understand better .
EXAMPLE
Here we directly output the sum via cout statement , because sum was returning a value and we could have either stored it in a variable and then cout it like written below
int s=sum();
cout<<s;
or the way it is done in example both are correct way of writting it .
FUNCTION WITH RETURN TYPE AND PARAMETER
SYNTAX
datatype function_name(parameter,parameter)
{
statements ;
}
Now return types means any datatype except “void” and parameter means there will be parameters . Now consider this example it will help you to understand better .
EXAMPLE