About return
. When a program is run, there is always another process that
he is the one who throws it. The terminal itself can be the process that launches your
program, the operating system if it is a system service, etc. In case of
Dev-C ++ (or the IDE you're using), I can assume that the process
Dev-C ++ launches a new terminal, and that terminal process is who launches
your program.
The return
serves to return, to the process "that you threw to me", a value
indicating whether the process ended satisfactorily or not. Return 0
means
that your program ended successfully, and a different value of 0
It means no.
For example, suppose you have a program that receives a value from the user, which
it must be a number greater than zero for its natural operation. If it is received
a number >0
, the program does what it has to do, and if not, the program can not do its homework, so it is considered a
wrong situation and it is notified. The code would be, for example, the following:
int main()
{
int i;
std::cin >> i;
if (i <= 0)
return 1; // Problema.
else {
// Código para el funcionamiento normal del programa.
return 0; // El programa terminó satisfactoriamente.
}
}
In this way, when your process is executed, when it finishes, the process that has
call to your program you can know if your program ended satisfactorily or
do not. Obviously, you can return the value you want. For example, you can
indicate different return values to indicate the type of specific error:
int main()
{
int i;
std::cin >> i;
if (i == 0)
return 1; // Se recibió un cero.
else if (i < 0)
return 2; // Se recibió un uno.
else {
// Código para el funcionamiento normal del programa.
return 0; // El programa terminó satisfactoriamente.
}
}
With those return values, the calling process (if you know what each means
return value of your program), can act in one way or another according to the
specific error produced.
If your program does not have a return
sentence, the compiler adds you a
return 0;
at the end of your program:
int main()
{
// Código del programa.
// return 0; // Implícito si no lo escribes tú.
}
Now, what is the system("pause")
worth? The system
function allows you to execute
any command that accepts the terminal language of your system
operative For example, in Windows, within system
, I can put any
command in DOS language. On Linux, you could write any command written in
Bash , for example.
Why do you need system("pause")
? As it turns out, Dev-C ++ , by default,
When you finish your program, close the terminal that Dev-C ++ opened for
Run your program. When calling system("pause")
, the program crashes
until the user types something. In this way, you keep the terminal open
and you can see the result of your program until you click on a button.
In CodeBlocks , if I'm not wrong, you would not need the system("pause")
, given
that CodeBlocks , by default, does not close the terminal when it finishes
program.
Conclusion: The fact that your program is closed, and you do not see the result,
it's not because any kind of exception is being thrown, but because you
program ends correctly and Dev-C++
has closed the terminal. The
value of return
has nothing to do here. You could remove it (the compiler
you would re-add it), or change the return value, that the result you will see
it will be the same. system("pause")
allows you to make your program wait for
the user presses a key, and therefore, keeping the terminal
open system("pause")
only you need in some IDEs ( Dev-C++
) and
pause
is an exclusive Windows system command. On Linux pause
no
It exists.
NOTE: A non-zero return value does not mean that your program contains errors, or that the program has failed from an internal point of view. The program ends well from a fully functional point of view regardless of the return value. That is, the process ends cleanly, just that a non-zero return value indicates that "the process has finished well but has not been able to do what it is supposed to do".