User Tools

Site Tools


fortran_module

Fortran Module

General form

The general form is ~~~~~~~~~~~ {: lang=fortran } module <name>

   
 
contains
 
end module <name> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  1. Data access ##

There are three possible access properties: `public, private, protected`.

  • `public`: Outside code has read and write access.
  • `private`: Outside code has no access.
  • `public, protected`: Outside code has read access.
  1. Using module in other code ###

One can include the module's public data in outside code. There are three ways.

  • `use <moduleName>`: includes all public data and methods
  • `use <moduleName>, <renames>`: includes all public data and methods, but renames some public data or methods
  • `use <moduleName>, only : <subset>`: includes only some public data and methods
  1. Examples ###

These examples highlight the above methods using the following module.

~~~~~~~~~~~ {: lang=fortran } !> \\file test_module.f module test_module

 implicit none
 private
 integer, public :: a=1
 integer, public, protected :: b=1
 integer, private :: c=1
end module test_module ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  1. Data access ####

~~~~~~~~~~~ {: lang=fortran } !> \\file main.f program main

 use test_module
 ! accessing public object works
 print *, a
 ! editing public object works
 a = 2
 ! accessing protected object works
 print *, b
 ! editing protected object does not work
 !b = 2 <- ERROR
 ! accessing private object does not work
 !print *, c <- ERROR
 ! editing protected object does not work
 !c = 2 <- ERROR

end program main ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  1. Using module in other code ####

~~~~~~~~~~~ {: lang=fortran } !> \\file main1.f program main

 use test_module
 print *, a, b

end program main ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

~~~~~~~~~~~ {: lang=fortran } !> \\file main2.f program main

 use test_module, better_name => a
 ! new name use available
 print *, better_name
 ! old name is not available anymore
 !print *, a  <- ERROR

end program main

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

~~~~~~~~~~~ {: lang=fortran } !> show using only program main

 use test_module, only : a
 ! only a is loaded
 print *, a
 ! b is not loaded
 !print *, b  <- ERROR

end program main

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

(FtrnWiki)

Research Fortran More

Fair Use Sources

Fortran: Fortran Fundamentals, Fortran Inventor - Fortran Language Designer: John Backus of IBM in 1957 (see John Backus Oral History); Modern Fortran - Legacy Fortran, Fortran keywords, Fortran data structures - Fortran algorithms, Fortran syntax, IBM Mainframe DevOps, Fortran DevOps, Fortran Development Tools (Fortran IDEs and Code Editors, Fortran Compilers, Fortran CI/CD Build Tools, Fortran Standard Library), Fortran Standards (ISO Fortran: 202X, 2018, 2018, 2008, 2003, 95, 90, 77), ANSI Fortran- 66, Fortran and Supercomputers (Fortran and High-Performance Computing (HPC)), Parallel Fortran (Embarrassingly Parallel Fortran - Fortran Coarrays), Fortran Paradigms (Imperative Fortran, Procedural Fortran, Object-Oriented Fortran - Fortran OOP, Functional Fortran), Fortran Community, Learning Fortran, Fortran on Windows, Fortran on Linux, Fortran on UNIX, Fortran on macOS, Mainframe Fortran, IBM i Fortran, Fortran installation, Fortran containerization, Fortran configuration, Fortran SRE, Fortran data science - Fortran DataOps, Fortran machine learning, Fortran deep learning, Fortran concurrency, Fortran history, Fortran bibliography, Fortran glossary, Fortran topics, Fortran courses, Fortran Standard Library, Fortran libraries, Fortran frameworks, Fortran research, Fortran GitHub, Written in Fortran, Fortran popularity, Fortran Awesome list, Fortran Versions. (navbar_fortran)


© 1994 - 2024 Cloud Monk Losang Jinpa or Fair Use. Disclaimers

SYI LU SENG E MU CHYWE YE. NAN. WEI LA YE. WEI LA YE. SA WA HE.


fortran_module.txt · Last modified: 2024/04/28 03:38 (external edit)