Path: utzoo!attcan!uunet!mcvax!hp4nl!philmds!leo From: leo@philmds.UUCP (Leo de Wit) Newsgroups: comp.lang.c Subject: Re: QuickC Message-ID: <768@philmds.UUCP> Date: 26 Aug 88 22:32:37 GMT References: <8808261424.AA11504@ucbvax.Berkeley.EDU> Reply-To: leo@philmds.UUCP (Leo de Wit) Organization: Philips I&E DTS Eindhoven Lines: 54 In article <8808261424.AA11504@ucbvax.Berkeley.EDU> TURGUT@TREARN.BITNET (Turgut Kalfaoglu) writes: | |The other day, I was struggling with QuickC - a very simply problem, |but really intriguing. Let me know if you can interpret this: | |main() |{ | int a,v; | a = 2; | v = square(a); | printf("%d\n",v); |} | |square(num) |int num; |{ | num = num*num; |} | |OK? There is no 'return' statement in the function. However, it works! |I get '4' as an answer. So I thought maybe it was keeping the result |of the last operation, so I added some dummy lines, | |square(num) |int num; |{ | int dummy; | num = num*num; | dummy=222; |} | |but the call STILL works... Can anyone shed some light onto this? |WHY does it work? |-turgut My guesses (I don't know QuickC, but I can understand why it would behave like this): In the first case: The return value of a C function is typically returned in a register (if it fits in one); often the compiler uses one and the same register. Also for temporary values mostly a small set of registers is used; if the temporary value num * num happens to be put into the same register, the above behaviour is explained. In the second case: The '222' can be directly assigned (to dummy) so that no temporary is needed; the num * num result is still in the 'temporaries' register. Other possible explanation: if the compiler is really smart it could see that dummy is assigned but never used and so optimized it away. Leo.