How to avoid declaring and setting the value of a variable in each subroutine?

雨燕双飞 提交于 2019-12-02 07:48:43

For global variables use modules (old FORTRAN used common blocks, but they are obsolete):

module globals
  implicit none

  integer :: n

contains

  subroutine read_globals()    !you must call this subroutine at program start

    print*, "enter n" ! this will be a constant value for the whole program
    read *, n
  end subroutine
end module

!this subroutine should be better in a module too !!!
subroutine Calcul(Time)
  use globals !n comes from here
  implicit none

  integer :: time 
  time = n*2
end subroutine

program test
  use globals ! n comes from here if needed
  implicit none

  integer :: time

  call read_globals()

  call Calcul(time)

  print*, time
end program

There are many questions and answers explaining how to use Fortran modules properly on Stack Overflow.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!