Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The use of return; For a beginner, from a different perspective c++

Tags:

c++

return

I've just started programming and I've come to hear the standard beginner's definition of "the use of the return value in main" a lot, but it does not get to the point I am trying to understand. So, yes a return value 0 for 'int main' for example signifies that the programme running was successful and since main is of int datatype, 0 reflects this.

BUT what is the point of this? Won't the computer already know that the code was successful or not? Surely, we could write a flawed code and then return 0, and by that logic, we (the programmers) are saying this code is correct but the compiler actually executes the programme and if it's wrong/flawed it simply cannot operate on it.

Please use explanations that a beginner could understand.

like image 649
user9078057 Avatar asked Nov 29 '22 08:11

user9078057


1 Answers

The return code of your program ain't about crashing, its about a functional kind of failure.

For example, the program grep defines exit/failure code 0 as successfully found and 1 as not found. While value 2 gets used for invalid input.

Within scripting, this can be used for some automated logic without the user needing a user to interpret the results.

As you are a beginner, I would recommend to always return zero as you are focusing on how to learn the language. Looking into how applications can connect to each other via exit codes is adding unneeded distraction/complexity.

like image 169
JVApen Avatar answered Dec 05 '22 17:12

JVApen