For Programmers: Free Programming Magazines  


Home > Archive > Fortran > July 2004 > Module.Scoping.Subroutine call not initializing variable









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Module.Scoping.Subroutine call not initializing variable
Sanjay Vohra

2004-07-13, 8:58 pm

Hi

I have a main, and modules called root root_function.
In main I call bisection(f,...) which is sitting in the
module root while f() is in root_function.
There is a variable iter internal to bisection whose value
is checked against a max value to decide if the looping is
to continue.
Now in main I call bisection twice, but on the second call
iter retains the previous value (the max in this case) and so
the loop inside bisection never gets executed. I don't understand
how can the value of iter be retained to the next call, even though
I set it to zero at the start of the bisection subroutine.

Thanks
--
Sanjay Vohra

MRDC 3408
gtg977e@prism.gatech.edu
James Giles

2004-07-13, 8:58 pm

Sanjay Vohra wrote:
....
> I have a main, and modules called root root_function.
> In main I call bisection(f,...) which is sitting in the
> module root while f() is in root_function.
> There is a variable iter internal to bisection whose value
> is checked against a max value to decide if the looping is
> to continue.
> Now in main I call bisection twice, but on the second call
> iter retains the previous value (the max in this case) and so
> the loop inside bisection never gets executed. I don't understand
> how can the value of iter be retained to the next call, even though
> I set it to zero at the start of the bisection subroutine.


It would be more useful for you to post a simple code
fragment rather than describing the problem. The likelyhood
is that you are not assigning zero to the variable ITER, but
are initializing it. Consider:

Subroutine XYZ(...)
integer :: iter = 0 ! an initialization
...
... code that changes ITER ...
...
end subroutine XYZ

In this code, the variable is initialized to zero. It is *not* assigned
zero each time the procedure is entered. The first time the procedure
is invoked, the value will be zero, on subsequent entries to the
procedure, the value of ITER will be whatever is was when you
last exited XYZ. This is because initialization automatically causes
the variable to have the SAVE attribute.

--
J. Giles

"I conclude that there are two ways of constructing a software
design: One way is to make it so simple that there are obviously
no deficiencies and the other way is to make it so complicated
that there are no obvious deficiencies." -- C. A. R. Hoare


Richard Maine

2004-07-13, 8:58 pm

gtg977e@prism.gatech.edu (Sanjay Vohra) writes:

> I don't understand
> how can the value of iter be retained to the next call, even though
> I set it to zero at the start of the bisection subroutine.


Standard comment #1 - show us instead of telling us. Namely, what do
you mean by I set it to zero at the start of the bisection
subroutine."

I'm betting that you did not, in fact, set it to zero at the start
of the bisection routine, but instead initialized it to zero. I'd
have to see the lines in question to know for sure.

Initialization does *NOT* necessarily happen at the beginning of
each call. (See several recent threads. Seems to be a hot topic
recently, though it isn't new at all.) If you want to set it to
zero at the beginning of each call, the way to do that is with
an executable assignment statement, not an initialization.

--
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
glen herrmannsfeldt

2004-07-13, 8:58 pm

James Giles wrote:
(snip)

> Subroutine XYZ(...)
> integer :: iter = 0 ! an initialization
> ...
> ... code that changes ITER ...
> ...
> end subroutine XYZ
>
> In this code, the variable is initialized to zero. It is *not* assigned
> zero each time the procedure is entered. The first time the procedure
> is invoked, the value will be zero, on subsequent entries to the
> procedure, the value of ITER will be whatever is was when you
> last exited XYZ.


At least a DATA statement doesn't look like an assignment statement...

Note, in C local variables are initialized each time, external
(global) variables are static and initialize once. This could be
a point of confusion for people coming to Fortran from C, or
using both at the same time.

In PL/I variables are AUTOMATIC (and initialized each time)
unless they are declared STATIC. The attribute is INITIAL,
making it at least slightly obvious that it is initialization
and not assignment. PL/I has multiple assignment making it
slightly easier to assign (I almost wrote initialize) multiple
variables at once A,B,C=0;

-- glen

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com