[comp.lang.fortran] Fortran functions

wje@siia.mv.com (Bill Ezell) (01/31/90)

After a long interval (18 years!) of not having looked at Fortran, I
have had to work on some VMS Fortran recently. I came across a situation
where I'm not sure how the compiler figures out what's going on. The
program does the following:

	character*5 foo
	character*5 ans

	ans = foo(arg)

where foo is a function, not in the same file. Foo is not declared external.

My question is, how does it know that the use of foo is a function call,
and not a subscript operation? It seems that this is rather ambiguous.
In the case above, arg was a character*80, so does the compiler infer
that foo() is a function call from the fact that a character string
can't be a subscript? If so, what if the call had been of the form

	ans = foo(1)

or some such? Also, is there any difference between:

	character*5 var
	character var*5
	character var(5)

Eagerly awaiting enlightenment....
-- 
Bill Ezell
Software Innovations, Inc.
wje@siia.mv.com
(603) 883-9300

johnl@esegue.segue.boston.ma.us (John R. Levine) (02/02/90)

In article <1990Jan30.191039.12165@siia.mv.com> wje@siia.mv.com (Bill Ezell) writes:
>	character*5 foo
>	character*5 ans
>	ans = foo(arg)
>
>My question is, how does it know that the use of foo is a function call,
>and not a subscript operation? It seems that this is rather ambiguous.

It's not ambiguous at all.  All arrays have to be dimensioned, either in
dimension statements or in common or data typing statements.  If it's not
dimensioned, it's not an array and is either a scalar or a function,
depending on whether you call it or not.  Although declaring functions to
be external is an excellent idea, you only need to do so in two cases: when
passing a function name as an argument to another function or subroutine,
and when the function name collides with an intrinsic.
-- 
John R. Levine, Segue Software, POB 349, Cambridge MA 02238, +1 617 864 9650
johnl@esegue.segue.boston.ma.us, {ima|lotus|spdcc}!esegue!johnl
"Now, we are all jelly doughnuts."

bethge@wums.wustl.edu (02/02/90)

In article <1990Jan30.191039.12165@siia.mv.com>, wje@siia.mv.com
(Bill Ezell) writes:
> 	character*5 foo
> 	character*5 ans
> 	ans = foo(arg)
> where foo is a function, not in the same file. Foo is not declared external.
> ... how does [the compiler] know that the use of foo is a function call,
> and not a subscript operation?

Simple. If FOO has not previously been declared as an array, then FOO()
cannot be a subscript operation, so it must be a function call.

The "CHARACTER*5 FOO" is *not* an array declaration, because in Fortran
a  character  string  is  *not*  simply  an array of characters, but is
rather a distinct type of object.  (Fortran is IMHO superior to several
other popular languages in this respect.)

This also answers your other question:
> ... is there any difference between:
> 	character*5 var
> 	character var*5
> 	character var(5)

The first two are identical, and are character strings of length 5. The
third  is  an  array  of  characters (actually, of character strings of
length 1), with dimension 5.  It is *not* the same.

If a character string were an array, it could  not  be  returned  as  a
function value, right?

_______________________________________________________________________
Paul H. Bethge                                    bethge@wums.wustl.edu
Washington University, St. Louis                     bethge@wums.bitnet