leif@ambone.UUCP (Leif Andrew Rump) (08/27/88)
TURGUT@TREARN.BITNET (Turgut Kalfaoglu) writes: >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? My answer has not been tested but I'm quite sure that this is the right answer to your question: When you call a function i C the parameters is put on the stack. You function uses the stack to keep the variable num and when the function return the result is popped from the stack - which (unfortunately(!) is the temporary variable num! Leif Andrew Rump, Ambrasoft A/S, Roejelskaer 15, DK-2840 Holte (Denmark) UUCP: leif@ambone.dk, phone: +45 2424 111; ABC BBS: +45 68 00 544 (2:505/38) Please note the node-change: Ambone, Italian for a pulpit (The following interpretations emerged Am_bone, Ambrasoft A/S bone when Ambone was put down on paper and Amb_one, Ambrasoft A/S #one accepted - they are solely mine!!!!!) ...
wes@obie.UUCP (Barnacle Wes) (08/31/88)
TURGUT@TREARN.BITNET (Turgut Kalfaoglu) writes: % square(num) % int num; % { % num = num*num; % } % % OK? There is no 'return' statement in the function. However, it works! % WHY does it work? In article <140@ambone.UUCP>, leif@ambone.UUCP (Leif Andrew Rump) writes: > When you call a function i C the parameters is put on the stack. You > function uses the stack to keep the variable num and when the function > return the result is popped from the stack - which (unfortunately(!) > is the temporary variable num! Turgut, what C compiler are you using? Many compilers treat one register as an 'accumulator' and do most of their 'temporary' arithmetic using that register. This same register is quite often used to pass the return value back to the caller for a function. On the 680x0, register d0 is quite often used. Probably what you have experienced is the compiler leaving the result of (num*num) in the register used to pass results, and the main program has picked up the value in this register. I certainly wouldn't count on it happening this way all the time, though. :-) :-O B-) ;-) 8-) -- {hpda, uwmcsd1}!sp7040!obie!wes "Happiness lies in being priviledged to work hard for long hours in doing whatever you think is worth doing." -- Robert A. Heinlein --
danr@hcx2.SSD.HARRIS.COM (09/07/88)
/* Written 11:45 am Aug 27, 1988 by leif@ambone.UUCP in hcx2:comp.lang.c */ /* ---------- "Re^2: QuickC" ---------- */ TURGUT@TREARN.BITNET (Turgut Kalfaoglu) writes: >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? My answer has not been tested but I'm quite sure that this is the right answer to your question: When you call a function i C the parameters is put on the stack. You function uses the stack to keep the variable num and when the function return the result is popped from the stack - which (unfortunately(!) is the temporary variable num! Leif Andrew Rump, Ambrasoft A/S, Roejelskaer 15, DK-2840 Holte (Denmark) UUCP: leif@ambone.dk, phone: +45 2424 111; ABC BBS: +45 68 00 544 (2:505/38) Please note the node-change: Ambone, Italian for a pulpit (The following interpretations emerged Am_bone, Ambrasoft A/S bone when Ambone was put down on paper and Amb_one, Ambrasoft A/S #one accepted - they are solely mine!!!!!) ... /* End of text from hcx2:comp.lang.c */
danr@hcx2.SSD.HARRIS.COM (09/07/88)
> /* Written 11:45 am Aug 27, 1988 by leif@ambone.UUCP in hcx2:comp.lang.c */ > /* ---------- "Re^2: QuickC" ---------- */ > TURGUT@TREARN.BITNET (Turgut Kalfaoglu) writes: > >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? > > UUCP: leif@ambone.dk, phone: +45 2424 111; ABC BBS: +45 68 00 544 (2:505/38) > /* End of text from hcx2:comp.lang.c */ I don't know whether any solution has been determined yet for this, because only one reply comes even close (that I have seen...). Here's my vote, considering what I learned in my computer science courses where we used Zenith AT-clones running ZENIX. The convention used by the C compiler on the 80*86 - based c compilers I have seen (Microsoft, Zenix, MS-DOS) calls for integers to be returned in the AX register. If the value of the square was allocated the AX register (which I believe you would see if you eyeballed the assembly), then that value would still be there when the function was returned. EVEN if no 'return ()' was explicitly encoded. The example using 'dummy' still works because the register allocator does its job and allocates different register(s) for that assignment (or probably does the job using a mov imediate to mem instruction, so no register is used). So, the moral of the story is that you were lucky this time, simply because of a quirk in the calling convention. I have seen this sort of behavior on other machines where the return value is in a register which may have been used for results, etc in the body of the function. -Dan Rittersdorf :: Please excuse those of us who are still learning the etiquete of the net if we break any sacred rules. I promise to look real sad if convicted of such a horrid act. :) (my FIRST smiley!!!!) :: Please also forgive the lack of a real nifty name, address, and disclaimer, as I have not yet taken the time. Instead, let me state that all views expressed above are in no way those of Harris Corporation, but are MINE, MINE, MINE!!! :: And last, but not least, let me say that although I may have a machine containing an Intel cpu on my desk at home, I would not wish my machine on anyone I like. Thank you.