# 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: 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:

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

The question should be specific, self-contained, and written in natural language.
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.
