Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Calling internal sub leads to strange results

0 views
Skip to first unread message

Martin Buchmann

unread,
Apr 14, 2003, 12:16:42 PM4/14/03
to
Hi all,

the subject could also be read as

'Calling internal sub drives me mad or F90 is mean' ;-) I'm convinced
that the pros will start laughing after looking at the code because
there must be some bloody stupid newbie mistake in it. Please find it :-)

I have basically something like this:

<--- test.f90 --->

PROGRAM test
IMPLICIT NONE

CALL swap()

CALL swap()

CONTAINS

SUBROUTINE swap()
IMPLICIT NONE

INTEGER :: sum_x = 0

INTEGER :: i

DO i = 1,10
sum_x = sum_x + i
WRITE (*,*) i, sum_x
END DO

RETURN

END SUBROUTINE swap

END PROGRAM

<----->

A small programm which contains a small internal subroutine. In this sub
there is a loop doing some addtions. As you see i declared the int sum_x
and set it to '0'. I though this would mean that sum_x is set to '0'
each time the sub is called. But when i look at my results:

1 1
2 3
3 6
4 10
5 15
6 21
7 28
8 36
9 45
10 55
1 56
2 58
3 61
4 65
5 70
6 76
7 83
8 91
9 100
10 110

I guess there is something i have not understood yet :-( I checked with
two different compilers so it is definetly my fault, but where?

Any hints are welcome,
Martin

--
The nice thing about standards is that there are so many of them to
choose from.
-- Andrew S. Tanenbaum

Richard Maine

unread,
Apr 14, 2003, 12:27:26 PM4/14/03
to
Martin Buchmann <Martin_...@gmx.net> writes:

> i declared the int sum_x
> and set it to '0'. I though this would mean that sum_x is set to '0'
> each time the sub is called.

I'm afraid that you thought wrong on that, which is the crux of your
problem. This subject comes up here fairly frequently. If you want
to set sum_x to 0 each time the subroutine is called, use an
assignment statement. That's what they do. Each time a subroutine is
called, the executable statements in it are executed.

What you have is initialization, which sets sum_x to 0 the *FIRST*
time (and only the first time) that sum_x is called. There is no
other way to do that.

Note that this has nothing in particular to do with internal
subroutines; it is the same for any kind of subroutine (or
function).

This also doesn't have a lot to to do with f90, though there are some
side issues that do. The initialization is just another syntax for
the same thing as a DATA statement did in f77. This particular syntax
is new to f90, but the functinality is not. One other part new to f90
is that (as discussed here recently, though not for the first time) in
f90 the initialization implies SAVE so that sum_x retains its value
between calls. In f77, the second call would be illegal (internal
procedure and syntax details aside) because sum_x would be undefined
on the second call unless you explicitly added a SAVE statement.

--
Richard Maine | Good judgment comes from experience;
email: my first.last at org.domain | experience comes from bad judgment.
org: nasa, domain: gov | -- Mark Twain

Martin Buchmann

unread,
Apr 14, 2003, 2:29:29 PM4/14/03
to
Richard,

ahh, thanks. That sounds reasonable to me.

I'm getting just confused very often because i'm switching between Perl
and Fortran which is not a nice thing sometimes. Although i'm not a real
perlhacker, i.e. i declare my variables even in Perl ;-)

Best Regards,
Martin

--
Smiley #7 The Shocked Smiley :-o
Uh-Oh!
You don't believe you said that.
Ask me to tell you more.

Catherine Rees Lay

unread,
Apr 15, 2003, 10:02:09 AM4/15/03
to
In article <3E9ADEEB...@gmx.net>, Martin Buchmann
<Martin_...@gmx.net> writes
Initialisation means just once - sum_x is already set to zero the first
time you enter the subroutine. The second time, it still has the value
left over from the first time. If you want to set it to 0 as the first
action each time you enter the routine, you should write your code to do
so explicitly in an executable statement (i.e. sum_x = 0 as the first
line of the subroutine).

HTH,
--
Catherine Rees Lay

Martin Buchmann

unread,
Apr 16, 2003, 4:02:58 PM4/16/03
to
Catherine,

thanks for your answer. Richard Maine already explained what i have to
do. The program is working since yesterday :-)

Nevertheless thanks and best regards
Martin

--
Word wird erst dann interessant, wenn es Patches gibt, die
die systematische Folterung von Karl Klammer ermoeglichen.
-- Axel Reichert in de.comp.text.tex

0 new messages