I did a little experimenting while doing some self taught C++ and found that after removing a return statement from a function, executing my compiled code runs the same. Here's a little example code -- if I were to remove the line
return numb;
the code runs just as if it were there
#include <iostream>
using namespace std;
class TheClass {
public:
int aNumber(int n) {
numb = n;
return numb;
}
void printOnScreen() {
cout << numb << endl;
}
private:
int numb;
};
int main() {
TheClass exampleObject;
exampleObject.aNumber(5);
exampleObject.printOnScreen();
return 0;
}
Which leads to my question, when is return needed, and when is it not, and also why?