0

about function

why if we change the int ( which is showed in parenthesis) to void for below code then output shows compilation error ?? #include <iostream> using namespace std; (int) timesTwo(int x) { return x*2; } int main() { cout << timesTwo(8) << endl; cout <<timesTwo(5) << endl; cout <<timesTwo(42) << endl; }

3rd Nov 2017, 6:44 PM
Amir Mehrabi
2 Answers
+ 7
void can't return any value, it just performs some tasks for the caller function. If you want to make it void, then print the result within it. void timesTwo(int x) { cout << x*2 << endl; } You can see this post if you are confused about when we need to return any value ^_^ https://www.sololearn.com/Discuss/342545/?ref=app
3rd Nov 2017, 6:47 PM
Shamima Yasmin
Shamima Yasmin - avatar
+ 1
Thank you so much
3rd Nov 2017, 7:33 PM
Amir Mehrabi