Value, value, who's got the value?

Bill Sears bills at sequent.UUCP
Fri Apr 28 07:35:21 AEST 1989


In article <1044 at itivax.iti.org> scs at vax3.iti.org (Steve Simmons) writes:
>
>Several questions: why does the OS make a difference; why does
>System V get it 'right' (even tho the code is wrong);
>
Actually, there is no "right" output for this program.  According to
K&R (version 1, sorry) pp23-24:  "a 'return' statement with no expression
causes control, but no useful value, to be returned to the caller, as
does "falling off the end" of a function by reaching the terminating
right brace."
Some compilers pass return expressions back through registers.  If no
explicit value is returned, the value which is used is whatever value
is in the register when it is evaluated.  Evidently, the SYSV compiler
is using the return register to do the computation that you specify in
the function.  Since no explicit value is returned, the value that is
left in the register when control returns to the caller is the value
of the expression in the function.
>
>why do none of these flag func2 as having a syntax error?
>
Because func2 doesn't have a syntax error.  C treats an expression
the same way it treats a statement so that functions that return
a value which is sometimes ignored don't fail.  For example:  The
close(2) system call returns a value (and hence is an expression)
to indicate the success or failure of the operation.  But most
programs do not check the return value (what are you supposed
to do?  The usual way close will fail is if the supplied file
descriptor is not open.  In which case the outcome is the same,
the fd is closed :-).



More information about the Comp.lang.c mailing list