I just started learning c++ by using bucky's tutorial on youtube and while i was watching his tutorial a question popped up.In the void function i cout the answer in myfunction while in the int function i cout the answer in int main ?Is there a difference between the 2???
void myfunction(int x, int y){
int answer;
answer=x+y; cout<<answer;
}
int main(){
myfunction(2,3);
system("pause");
return 0;
and
int myfunction(int x, int y){
int answer;
answer=x+y; return answer;
}
int main(){ cout<<myfunction(2,3);
system("pause");
return 0;
It all depends on what you intend to have your function do. I personally would choose the approach where the myfunction returns the result instead of outputting it via std::cout itself, as that makes your function have more potential purposes and be more reusable.