3

I am currently writing my own programming language, mainly for educational purposes.

When writing a simple hello world example, I realised that many programming languages take the following form:

write "hello world" to the console
return 0
  • Do all programs have to return an integer somehow?
  • Do those that don't explicitly require you to state a return value implicitly return 0 anyway?
  • Does this change between popular operating systems based upon Windows NT, Linux Kernel or Mac OS Xs kernel?
  • If so, why?

I am unsure what to tag this question as, any help is appreciated of course.

OMGtechy
  • 7,082
  • 8
  • 40
  • 76
  • 1
    I believe these are usually 0 = success. Anything else is an error. For example in Windows you have http://msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v=vs.85).aspx and in Linux you have http://stackoverflow.com/questions/1101957/are-there-any-standard-exit-status-codes-in-linux. Also http://en.wikipedia.org/wiki/Exit_status – Daniel Williams Sep 03 '13 at 22:54
  • @DanielWilliams I thought so too, but does it HAVE to return something or will it break the requirements of certain popular operating systems if it does not? – OMGtechy Sep 03 '13 at 22:55
  • I think to be fully functional your program should return an exit status. If it doesn't it probably defaults to 0 anyways. – Daniel Williams Sep 03 '13 at 22:56
  • @DanielWilliams once again I agree, it's the "probably" part I'm trying to resolve. Thank you though :) – OMGtechy Sep 03 '13 at 23:05
  • 1
    If the OS specifies that you must supply a return value, then you must. That's the only answer that counts. – Mark Ransom Sep 03 '13 at 23:20
  • @MarkRansom Very true. Do you know of a good place I could find such information? Googling "do all (insert operating system) programs need a return value" doesn't show much useful. – OMGtechy Sep 03 '13 at 23:22
  • Unfortunately you're not going to find an answer for *all* operating systems, the question is too broad. I can point you to some Windows documentation: http://msdn.microsoft.com/en-us/library/windows/desktop/ff381406%28v=vs.85%29.aspx. Notice that the actual entry point into the program is called `main` and is established by the linker, `WinMain` is called after `main` has finished any necessary initialization. – Mark Ransom Sep 04 '13 at 02:28
  • It seems like it's part of the UNIX specification that the `exit()` system call should be called with a status, so I'd say it's required. http://pubs.opengroup.org/onlinepubs/7908799/xsh/_exit.html – Yunchi Oct 13 '13 at 05:20

1 Answers1

1

It's a OS specific question, so no-one can answer if it's true for all OS. That said, I believe that, in any reasonable OS, yes - a program has to have a return value.

Why? To chain the programs execution and therefore to report problems, relaunch and so on. There aren't programs that don't return a value, it's just a compiler that hides it from a programmer.

Is this the case on all major operating systems? Probably. 0 usually means means success but you shouldn't make such assumptions when writing your programs; use language constants.

If you write your own language you may map your error codes (exceptions, termination reasons) to the host language's error codes.

OMGtechy
  • 7,082
  • 8
  • 40
  • 76
piotrek
  • 12,111
  • 8
  • 63
  • 144