Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Path: utzoo!mnetor!uunet!husc6!uwvax!oddjob!gargoyle!ihnp4!chinet!dag
From: dag@chinet.UUCP (Daniel A. Glasser)
Newsgroups: comp.lang.c
Subject: Re: Check for function results
Message-ID: <1913@chinet.UUCP>
Date: Thu, 26-Nov-87 00:41:54 EST
Article-I.D.: chinet.1913
Posted: Thu Nov 26 00:41:54 1987
Date-Received: Sun, 29-Nov-87 23:16:00 EST
References: <10530@brl-adm.ARPA>
Reply-To: dag@chinet.UUCP (Daniel A. Glasser)
Organization: Chinet - Public Access Unix
Lines: 83
Summary: Read K&R, Chapter 4, section 2

In article <10530@brl-adm.ARPA> I1090801%DBSTU1.BITNET@CUNYVM.CUNY.EDU writes:
>I am a student of computer science and I have to write a C-compiler.
>The problem I came across is: does a C-compiler have to generate code
>to check if a function really delivers a result in all cases?
[Lots of stuff deleted]
>I wonder if all this is really necessary, but I can't find anything
>about this problem in K&R. Do other compilers check this? I think,
>some strange errors can be detected by this method.
>
>Maybe someone on the net can help me.
>
>                                      Ulf Gruene

Well, Ulf, maybe I can help you -- K&R explicitly covers this in chapter 4.
In section 1 (Basics) (in my book, it's page 68) there is the following:
(Reproduced without permission)

     "The return statement is the mechanism for returning a value from
    the called function to its caller.  Any expression can follow return:

	return ( expression )

    The calling function is free to ignore the returned value if it wishes.
    Furthermore, there need be no expression after return; in that case, no
    value is returned to the caller.  Control also returns to the caller with
    no value when execution "falls off the end" of the function by reaching
    the closing right brace.  It is not illegal, but probably a sign of
    trouble, if a function returns a value from one place and no value from
    another.  In any case, the "value" of a function which does not return
    one is certain to be garbage.  The C verifier lint checks for such errors.

The scheme that you propose introduces a large amount of overhead.
The compiler knows when it is transfering control to the function
epilogue (return) code, and knows how it gets there.  You could make
the compiler generate a warning (if the function is not declared void)
if you like.

The ANSI draft standard covers the return statement in section 3.6.6.4.
The text of that portion is (also quoted without permission):

   3.6.6.4  The return statement

   Constraints

	A return statement with an expression shall not  appear
   in a function whose return type is void.

   Semantics

	A return statement terminates execution of the  current
   function  and returns control to its caller.  A function may
   have any number  of  return  statements,  with  and  without
   expressions.

	If a return statement with an expression  is  executed,
   the  value  of the expression is returned to the caller.  If
   the expression  has  a  type  different  from  that  of  the
   function  in which it appears, it is converted as if it were
   assigned to an object of that type.

	If  a  return  statement  without   an  expression   is
   executed,  and the value of the function call is used by the
   caller, the behavior is  undefined.   Reaching  the  }  that
   terminates  a  function  is equivalent to execution a return
   statement without an expression.

I hope that this information helps.  Remember structure passing,
returns and assignment, enumerated types and pointers to void.
Otherwise, only 90% of the C code in the world will work with
your compiler.
						Daniel A. Glasser

Disclaimer:

   The contents of this posting are subject to change without notice.
   void where prohibited by law.  int everywhere else.
   The above is not the truth.  I don't know the truth.
-- 
					Daniel A. Glasser
					...!ihnp4!chinet!dag
					...!ihnp4!mwc!dag
					...!ihnp4!mwc!gorgon!dag
	One of those things that goes "BUMP!!! (ouch!)" in the night.