Re: local variable initilization



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


.



Relevant Pages

  • Re: local variable initilization
    ... end subroutine subA ... the default for the values of all the local variables and arrays in a ...
    (comp.lang.fortran)
  • Re: io-unit has been opened
    ... subroutine suba() ... end subroutine suba ... If it is "illegal" to open the same file twice, then the Intel Fortran compiler has a bug. ... Support the Original G95 Project: http://www.g95.org ...
    (comp.lang.fortran)
  • Re: io-unit has been opened
    ... subroutine suba() ... end subroutine suba ... opening the same io-unit twice, and second, you're opening the same file twice. ...
    (comp.lang.fortran)
  • local variable initilization
    ... end subroutine subA ... second output in main: 100 ... which shows "the values of all the local variables and arrays in a ...
    (comp.lang.fortran)
  • Re: io-unit has been opened
    ... subroutine suba() ... end subroutine suba ... opening the same io-unit twice, and second, you're opening the same file twice. ... you use the INQUIRE statement. ...
    (comp.lang.fortran)