Just stared

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;

}
 
int myfunction(int x, int y)

returns an integer return answer; then:
 
cout<<myfunction(2,3);

it's printing an integer
in this case answer

in:
1
2
3
4
5
6
void myfunction(int x, int y){

int answer;
answer=x+y;
cout<<answer;
}


your function doesn't
return anything but
calculates answer=x+y;
and then:
print: answer
cout<<answer;
so basically one returns the answer and the other one prints it out?
Yes.
ok final question is there a reason to use one over the other?Or does both get the job done just in a different way?
Last edited on
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.

-Albatross
Ty all that cleared up a lot of questions i had.
Topic archived. No new replies allowed.