Fortran

Guide To Learn

Defining the main program

The main program is the fundamental program unit in Fortran. It allows you to assign a name to your program and defines the program scope, as shown in the following listing.

Listing 2.3 Defining the program unit and scope

program tsunami         ❶
end program tsunami     ❷

❶ Begins the new program and gives it a name

❷ Ends the program

Assigning a name to a program doesn’t do anything in practice, but it can help you stay organized when you start working with dozens of different programs.

Compiling and linking a main program source file results in an executable file that you can invoke from the host operating system (see figure 2.1). You can’t invoke a main program from other program units.

What other program units are there?

Here, I give you a sneak peek of what’s coming in chapters 3 and 4. Different program units can together form an executable program or a nonexecutable library:

  • Main program –Top-level unit that can be invoked only from the operating system
  • Function –An executable subprogram that is invoked from expressions and always returns a single result
  • Subroutine –An executable subprogram that can modify multiple arguments in-place but can’t be used in expressions
  • Module –A nonexecutable collection of variable, function, and subroutine definitions
  • Submodule –Extends an existing module and is used for defining variable and procedure definitions that only that module can access; useful for more complex apps and libraries

For now, we can work with only the main program. We’ll dive deep into functions and subroutines in chapter 3, and modules in chapter 4.

The program statement is not mandatory. It can be useful to omit it for short test programs. However, it’s good practice to include it and pair it with a matching end program statement. Technically, end is the only required statement for any Fortran program. That statement also makes the shortest possible, though useless, Fortran program.

Tip Always pair the program statement with a matching end program statement.

Defining the main program

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top