Re^2: QuickC

Barnacle Wes wes at obie.UUCP
Wed Aug 31 13:12:55 AEST 1988


TURGUT at 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 at ambone.UUCP>, leif at 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 --



More information about the Comp.lang.c mailing list