Skip to content

Instantly share code, notes, and snippets.

@jonathanschilling
Created June 9, 2021 12:55
Show Gist options
  • Save jonathanschilling/7a1baf3e4ddb1d726575e00408fd02f2 to your computer and use it in GitHub Desktop.
Save jonathanschilling/7a1baf3e4ddb1d726575e00408fd02f2 to your computer and use it in GitHub Desktop.
Initialization of a variable in Fortran at declaration vs. at runtime
program test_fortran_init
implicit none
print *, "test with init at declaration"
call test_routine
call test_routine
print *, "test with init at subroutine start"
call test_routine_2
call test_routine_2
end program
subroutine test_routine
implicit none
integer :: var = 0
print *, "var = ", var
var = var + 1
print *, "var = ", var
end subroutine
subroutine test_routine_2
implicit none
integer :: var
var = 0
print *, "var = ", var
var = var + 1
print *, "var = ", var
end subroutine
! expected screen output:
! test with init at declaration
! var = 0
! var = 1
! var = 1
! var = 2
! test with init at subroutine start
! var = 0
! var = 1
! var = 0
! var = 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment