2

I'm helping in a project of my course in the college and we have a script that depends of the return value of the main function of one of its modules (by main function, I mean the "main()"). But in that function, no value is returned either the code has done successfully what it was supposed to be done, or not. So, if there's no "return" statement in the function, what's the standard value to be returned if it reaches the end of the function?

João H. L.
  • 33
  • 1
  • 5
  • You shouldn't have to return anything from that particular function. If you *want* it to return something from completion of the script, look into [this question](http://stackoverflow.com/questions/285289/exit-codes-in-python) instead. – Makoto Nov 10 '14 at 16:51
  • He is my friend!! In the real life! = ) – Thi G. Nov 12 '14 at 11:54

2 Answers2

9

You've already made a false assumption from the start, which is that there is necessarily a main function at all. There isn't: Python doesn't need one, and doesn't call one automatically if it's there. Rather, all Python modules are executed from top to bottom; if the only thing at module level is a call to main(), then that is what is called, but otherwise it will not happen.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
3

Everything Daniel said is true, but additionally if you want to know the process' exit code, it will by default be 0 unless an uncaught exception is thrown in which case it will be non-zero. You can modify the exit code directly by invoking sys.exit.

b4hand
  • 9,550
  • 4
  • 44
  • 49