I'm learning C++. I'm confused about return types -- why is it better to use a return type of "void" sometimes, and a return type of "int" other times?
void doesn't return anything.so if you don't have to return use void else you could use int/string/boolean if you had to return value from method
To expound on the answer given by @hba , you will generally use a function as a tool for manipulating or evaluating data given by another function. If your program has an 'add()' function that adds to variables and returns the sum, you would need to have a "return type" that was descriptive of what the sum was (ie float, double, int). However if you then took that sum and passed it to a 'print()' function, that simply prints to the screen, there is no return value needed and thus your 'print()' would have a return type of 'void'. So one type is not "better" than the other, they are used to describe the data types that are passed between functions.
Thank you! I understand now. :)
Join our real-time social learning platform and learn together with your friends!