Path: utzoo!utgpu!attcan!uunet!lll-winken!lll-tis!ames!oliveb!sun!thetone!swilson From: swilson%thetone@Sun.COM (Scott Wilson) Newsgroups: comp.lang.c Subject: Re: QuickC Message-ID: <65868@sun.uucp> Date: 26 Aug 88 20:01:01 GMT References: <8808261424.AA11504@ucbvax.Berkeley.EDU> Sender: news@sun.uucp Reply-To: swilson@sun.UUCP (Scott Wilson) Organization: Sun Microsystems, Mountain View Lines: 35 In article <8808261424.AA11504@ucbvax.Berkeley.EDU> TURGUT@TREARN.BITNET (Turgut Kalfaoglu) writes: >main() >{ > int a,v; > a = 2; > v = square(a); > printf("%d\n",v); >} > >square(num) >int num; >{ > num = num*num; >} >WHY does it work? I get the same behavior on a Sun running 3.5 (but not 4.0). The reason it works is that the scratch register used for the multiplication happens to be the same register that is used to return the function's value (in my case d0). The reason that adding the assignment to dummy didn't change anything is that this most likely didn't involve any registers, the value was stored directly in memory since dummy is on the stack. Under SunOS 4.0 this gives the answer 0. For some reason all functions without an explicit return statement return 0. I think it has something to do with compatibility with 4.3 BSD where return(x) in main() is the same as exit(x). Since main() often ends without a return (or exit() being called) it could cause problems if a random register value were used for the exit code. So, main() without a return returns zero which is a "successfull" termination in UNIX. -- Scott Wilson arpa: swilson@sun.com Sun Microsystems uucp: ...!sun!swilson Mt. View, CA