Re: local variable initilization
- From: Herman D. Knoble <SkipKnobleLESS@xxxxxxxxxxxxxxx>
- Date: Thu, 24 Apr 2008 07:29:19 -0400
On Wed, 23 Apr 2008 20:37:45 -0700 (PDT), Mike <SulfateIon@xxxxxxxxx> wrote:
-|Hi
-|
-|module A
-|contains
-|subroutine subA()
-|real :: x
-|print *,'before adding',x
-|x=x+1
-|print *,'after adding',x
-|end subroutine subA
-|end module A
-|program main
-|use A
-|x=100.
-|call subA()
-|print *,'first output in main:',x
-|call subA()
-|print *,'second output in main:',x
-|end program main
-|
-|The results are (CVF6.6c used):
-|before adding 0
-|after adding 1
-|first output in main: 100
-|before adding 1 <=== why?
-|after adding 2
-|second output in main: 100
-|
-|Why? Isn't x in subA a local variable? I read the Chapman's book,
-|which shows "the values of all the local variables and arrays in a
-|procedure become undefined whenever we exit the procedure." I thought
-|x=0. x doesn't have SAVE attribute in subA.
-|
-|Mike
Mike, You might move your program's 4th line up two lines. That is:
module A
real :: x=0
contains
subroutine subA()
print *,'before adding',x
x=x+1
print *,'after adding',x
end subroutine subA
end module A
program main
use A
x=100.
call subA()
print *,'first output in main:',x
call subA()
print *,'second output in main:',x
end program main
.
- References:
- local variable initilization
- From: Mike
- local variable initilization
- Prev by Date: Re: Fortran templates
- Next by Date: Re: local variable initilization
- Previous by thread: Re: local variable initilization
- Next by thread: Re: local variable initilization
- Index(es):
Relevant Pages
|
|