From the course: Introduction to Fortran

Structure of a Fortran program - FORTRAN Tutorial

From the course: Introduction to Fortran

Structure of a Fortran program

- [Instructor] In this lesson, we'll describe the basic structure of a Fortran program. There are a variety of sections to a program and they must come in a specific order. Let's take a look at what they are and a few details about them. This is the basic structure of a Fortran program. The only part here that is strictly required though is the end keyword. The first program statement is optional but I'd recommend including it anyways. Fortran has modules and use statements are how you access stuff inside of them. We'll cover them in more detail later in the course but they must be the first statements in a program if there are any. Next, an implicit none statement. There's some historical importance to this that I'll cover in a moment, but it comes after any use statements and before anything else. Next comes what in the standard is referred to as the specification section. This is where variables are declared as well as a couple of other things that we won't really have time to discuss in this course. Once you've declared all your variables comes the meat of the program, the executable section. This is where statements that actually do things go. Finally, if you have procedures you'd like to define in the program, they go at the end after a contained statement. Let's also talk about another important feature of Fortran. It is case insensitive. That means that each of these lines is exactly equivalent as far as a Fortran compiler is concerned. That doesn't mean that you as the writer shouldn't be careful about how you use case because it can be very important to human readers. I recommend sticking to mostly lowercase for most things. Additionally, white space is mostly insignificant. Thus, the code on the left side here is equivalent to the code on the right. Again, that doesn't mean that you should ignore white space because as you can see, it can be quite helpful for human readers. You can also include content in your source code that the compiler will ignore. These are called comments and anything following an exclamation point will be ignored by the compiler. There is a lot of debate about how many comments should be included in most source code. I'll simply say that my opinion on the matter is that you should first attempt to write the code in a way that the comment would be superfluous because the compiler doesn't check your comments. Now let's come back to that implicit none statement. This statement tells the compiler to ensure that every variable you use has a declaration. If you don't include this statement, then the compiler assumes the type of an undeclared variable based on the first letter of its name. Thus a typo in a variable name somewhere could lead to weird and hard to find bugs. You should always include an implicit none statement at the beginning of your programs and modules. That covers the basic outline of a Fortran program and some general guidelines for writing Fortran code.

Contents