+ 1

Why we need to return a value from a function?

20th Jan 2017, 10:06 AM
Dawood Shahat
Dawood Shahat - avatar
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.
20th Jan 2017, 10:16 AM
Harsimran Kaur Bindra
Harsimran Kaur Bindra - avatar
+ 3
Because the return type tells the compiler what type of data is the function going to return after performing the manipulation
20th Jan 2017, 10:18 AM
Harsimran Kaur Bindra
Harsimran Kaur Bindra - avatar
+ 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
20th Jan 2017, 10:20 AM
Harsimran Kaur Bindra
Harsimran Kaur Bindra - avatar
+ 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
20th Jan 2017, 10:14 AM
Aaron
Aaron - avatar
0
if this is the case then why we have the return type?
20th Jan 2017, 10:17 AM
Dawood Shahat
Dawood Shahat - avatar