Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do all programs have to have a return value?

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.

like image 640
OMGtechy Avatar asked Mar 25 '26 04:03

OMGtechy


1 Answers

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.

like image 158
piotrek Avatar answered Mar 26 '26 22:03

piotrek