Fortran Usage of Modules Using modules from other program units

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

To access entities declared in a module from another program unit (module, procedure or program), the module must be used with the use statement.

module shared_data
  implicit none

  integer :: iarray(4) = [1, 2, 3, 4]
  real :: rarray(4) = [1., 2., 3., 4.]
end module


program test

  !use statements most come before implicit none
  use shared_data

  implicit none

  print *, iarray
  print *, rarray
end program

The use statement supports importing only selected names

program test

  !only iarray is accessible
  use shared_data, only: iarray

  implicit none

  print *, iarray
  
end program

Entities can be also accessed under different name by using a rename-list:

program test

  !only iarray is locally renamed to local_name, rarray is still acessible
  use shared_data, local_name => iarray

  implicit none

  print *, local_name

  print *, rarray
  
end program

Further, renaming can be combined with the only option

program test
  use shared_data, only : local_name => iarray
end program

so that only the module entity iarray is accessed, but it has the local name local_name.

If selected for importing names mark as private you can not import them to your program.



Got any Fortran Question?