> For the complete documentation index, see [llms.txt](https://antoinearnoud.gitbook.io/fortran/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://antoinearnoud.gitbook.io/fortran/chapter3.md).

# Modules

## What is a module?

A module is a way to store variables and functions/subroutines that are available everywhere in the program and modifiable from anywhere in the program.

## How to write a module

To store variables that must be accessible everywhere in a program, you need to create a file called (for example) `mymodule.f90` which contains the following

```
module mymodule
  implicit none
  real :: a
  real :: b
  a = 5
  b = 6
  contains
    function functionInModule(x,y)
      implicit none
      real :: x, y, functionInModule
      functionInModule = x-y
    end function
end module
```

And the main program becomes

```
program myprogram
  use mymodule
  implicit none
  real :: w, z
  some code here
  w = 2
  z = functionInModule(w,w)
end program
```

Note the `use mymodule` at the beginning of the program before the `implicit none`. Because of this line, all the variables and functions/subroutines declared in the module can be used in the program.

One can also use `use mymodule, only: variable/function/subroutine` to make only part of the variables or functions available. For example

```
program myprogram
  use mymodule, only: functionInModule
  implicit none
  real :: w,z,r
  w = 1
  r = 2
  z = functionInModule(w,r)
end program
```

This can be done to make sure that other parameters contained in the module are not inadvertently modified in the program.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://antoinearnoud.gitbook.io/fortran/chapter3.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
