[net.lang.f77] when is a function not a function...

geoffs@brl-tgr.ARPA (Geoffrey Sauerborn ) (05/23/85)

c This might just be unique to the UNIX(tm) compiler,
c  or is it standard that functions can be CALLed
c  and subroutines be (almost) treated as functions?
	PROGRAM main
	CALL func1
	dummy = sub1()
	PRINT*,sub1()
	END

	FUNCTION func1
	dummy = 99.0
	PRINT*, " func1=",dummy
	func1 = dummy
	END

	SUBROUTINE sub1
	PRINT*,"I'm a subroutine!"
	END
C----------------------------------------------
C the results: using 4.2 BSD f77
C----------------------------------------------
C   func1=   99.0000
C  I'm a subroutine!
C  I'm a subroutine!
C  0.

gwyn@brl-tgr.ARPA (Doug Gwyn <gwyn>) (05/24/85)

> c This might just be unique to the UNIX(tm) compiler,
> c  or is it standard that functions can be CALLed
> c  and subroutines be (almost) treated as functions?

The only difference between a function and a subroutine is that
one leaves a return value in a place that the caller can find it
(generally a machine register).  Of course it would be nice if
the compiler produced an error message when you misused the
language like this, but this is hard to do in general since the
function/subroutine could be in another (not yet written!) source
file and not enough information is available at link-edit time to
let the linker detect the usage error.

woods@hao.UUCP (Greg Woods) (05/24/85)

> c This might just be unique to the UNIX(tm) compiler,
> c  or is it standard that functions can be CALLed
> c  and subroutines be (almost) treated as functions?
> 	PROGRAM main
> 	CALL func1
> 	dummy = sub1()
> 	PRINT*,sub1()
> 	END

  You can do this, since as Doug Gwyn points out, this mis-usage is
difficult to detect. In fact, calling a function with a call statement is
a convenient way of invoking a function and ignoring the return value.
The other example (using a subroutine like a function) will, of course,
result in an unpredictable return value which may vary from one machine
to another or even one compiler to another on the same machine.

--Greg
-- 
{ucbvax!hplabs | allegra!nbires | decvax!noao | harpo!seismo | ihnp4!noao}
       		        !hao!woods

CSNET: woods@NCAR  ARPA: woods%ncar@CSNET-RELAY
   
     "...I may not be right but I've never been wrong
      It seldom turns out the way it does in the song..."

throopw@rtp47.UUCP (Wayne Throop) (05/25/85)

> > c This might just be unique to the UNIX(tm) compiler,
> > c  or is it standard that functions can be CALLed
> > c  and subroutines be (almost) treated as functions?
> > 	PROGRAM main
> > 	CALL func1
> > 	dummy = sub1()
> > 	PRINT*,sub1()
> > 	END
>
>   You can do this, since as Doug Gwyn points out, this mis-usage is
> difficult to detect. In fact, calling a function with a call statement is
> a convenient way of invoking a function and ignoring the return value.
> The other example (using a subroutine like a function) will, of course,
> result in an unpredictable return value which may vary from one machine
> to another or even one compiler to another on the same machine.
>
> --Greg

It may be convenient as suggested, but you hadn't better expect either
mistake to be portable.  Granted, the "CALL func" is more likely to
sneak by than the "dummy = sub1()", (and do something reasonable on many
machines) but some points are worth mentioning:

  - Some fortran compilers will catch these errors when the invocation
    occurs in the same file as the definition.
  - Some fortran compilers return function arguments in a "hidden"
    by-reference argument (usually an inserted argument 1).  Hence "CALL
    func" will cause about the same amout of greif as calling a
    subroutine with a missing first argument.
-- 
Wayne Throop at Data General, RTP, NC
<the-known-world>!mcnc!rti-sel!rtp47!throopw