My idea is to write a exit function to terminate the whole system, so I wrote the function int exit_c(), and put it in the while loop, but it seems that the system keeps working.
int main() {
while (true) {
exit_c();
}
system("pause");
return 0;
}
int exit_c() {
cout << "close" << endl;
system("pause");
return 0;
}
But when I replace the exit_c() in while loop, which just system("pause") and return 0, the system will terminate.
int main() {
while (true) {
system("pause");
return 0;
}
system("pause");
return 0;
}
return
returns from a function. Hence, you cannot simply copy the code in the function to replace its call and expect the code to behave the same. The return 0;
which before returned from exit_c
now returns from main
. Thats the difference you see.
On a related note: return 0;
in main
is implicit. You can remove it from your code when it is the last statement.