+ 1
Why we need to return a value from a function?
5 Respostas
+ 4
We use a function just to perform manipulation on a certain type of data which it receives as an argument.
Now in some cases we don't want to directly print the data obtained after manipulation to the user, but rather we want to perform even further manipulations on that data in either the main function or some other function. That is when we want the function to return the data.
+ 3
Because the return type tells the compiler what type of data is the function going to return after performing the manipulation
+ 3
And it is not necessary for a function to always return a value it can be made void in that case it will either print the result to the user or will simply assign values to a class variable
+ 1
You don't always need to, for example;
void Func (int x, double y){
cout << x*y;
}
int main (){
Func (3,5);
}
//Outputs 15
0
if this is the case then why we have the return type?