Fortran 90 Tutorial

0 views
Skip to first unread message

Mirtha Hinrichs

unread,
Aug 4, 2024, 2:36:25 PM8/4/24
to nielouiskydmi
Fortranwas originally developed by a team at IBM in 1957 for scientific calculations. Later developments made it into a high level programming language. In this tutorial, we will learn the basic concepts of Fortran and its programming code.

Fortran is a computer programming language that is extensively used in numerical, scientific computing. While outwith the scientific community, Fortran has declined in popularity over the years, it still has a strong user base with scientific programmers, and is also used in organisations such as weather forecasters, financial trading, and in engineering simulations. Fortran programs can be highly optimised to run on high performance computers, and in general the language is suited to producing code where performance is important.


Fortran is a compiled language, or more specifically it is compiled ahead-of-time. In other words, you must perform a special step called compilation of your written code before you are able to run it on a computer. This is where Fortran differs to interpreted languages such as Python and R which run through an interpreter which executes the instructions directly, but at the cost of compute speed.


Perhaps you have previously used other programming languages, such as Python, R, or MATLAB, which have developed with easy to understand syntax in mind, and with a programming style that favours more rapid development time at the expense of computational performance. Fortran will seem different to these languages in many ways, but the principles of programming remain broadly the same, and some syntax is shared or similar to elements of other programming languages.


The first three lines are comment lines - you will hopefully find a lot of these in Fortran programs you are given, and in the ones you write yourself. Comment lines are just notes or explanations that help the programmer and the user. They are never executed by the computer and you can write whatever you like within any line marked as a comment. In Fortran this is the exclamation mark (!). Any line beginning with an exclamation mark will be ignored by the computer when the program runs. Comments help the user to understand more complicated bits of code by providing a more human-readable explanation, or perhaps giving an example of how to use the code.


Begin the program! Fortran is quite a verbose language, in other words, we have to be quite explicit in telling it what we are about to do (Contrast with Python and R which are said to be more dynamic or intuitive languages and meaning can often be inferred.)


So here we are just telling Fortran that we wish to begin our program, and we can optionally give it a name. Skip down to the last line: notice how we also have an END PROGRAM statement. Fortran likes to keep things balanced and know exactly when you have ended sections of code. You will see the END statement used often to demarcate sections of the code such as loops, functions, and so on.


The next section of the program is where we define the variables to be used in the program. In Fortran programs, you will almost always see these defined at the very top of the program, unlike in other languages where you can define them as you go along. Fortran likes to know at the start which variables it is dealing with. We will cover what the different types are in the next section. Note also how we have added comments to explain in more human-readable format what each variable does.


This program would be saved like a plain-text file, but we would give it the extension .f90. (By convention). To run the program, we would first need to compile it, which is covered in a later section of the tutorial.


Within the PROGRAM statements, your Fortran program can define functions, declare variables to be used in these functions, just like in other programming languages such as R or Python. Within these statements, this is where the calculations on data are performed in the program.


Variables represent data or values used in your program. A variable can represent a single value, like the expression x = 3, or a variable can refer to a larger structure for holding data, such as a table or a list of values. Variables can also be used to store non-numeric values such as text, letters, and words, for example.


We refer to variables in Fortran as having certain Types. A type describes what kind of data the variable is expected to contain. For example, the expression x = 3 we could say that x refers to a number, i.e. the number 3. If we had something like: x = "cat", we might say that x refers to some characters or letters. These are examples of how we could describe our variables using keywords to keep track of what kind of data we are using.


In Fortran, we are required to be specific about what kind of data our variables are. Fortran has a set of keywords that are used to define the different types of variables. For example to define some INTEGER variables for use in a later calculation, we would write in Fortran:


We are saying with these statements that we want to create two integers, n and m and that they will be assigned the values 3 and 6 respectively. (We can delay assigning values until later on in the program, if necessary.)


Variables in fortran are usually defined in the first few lines of the program or a function, like in our example program above. There is one more type that is introduced in the example program, the LOGICAL type. This type refers to variables that are used as True or False values.


Variable names may be made up of standard latin-alphabet characters, underscores, and numbers. Note that Fortran is not case-sensitive. Keywords and variable names may be written in uppercase or lowercase. Fortran does not distinguish between uppercase and lowercase names, so be careful of using them in variable names. For example: Cond_1 is the same as cond_1.


By convention, you will often see Fortran keywords written in UPPERCASE, though this is not a requirement. For example, REAL, INTEGER, IF, ELSE, PROGRAM, and so on. You can chose to use either uppercase or lowercase, but it is good practice to be consistent in their use, for readability.


When you are running a Fortran program, the easiest way to see the results of the calculation or other outputs are to print them to the terminal, command line, or console. (These are all terms used interchangeably to refer to the same thing - the window where commands can be entered and text is printed out to screen.)


You also need a way of inputting data to the program. This can also be done (for this simple example program) by typing the values directly into the terminal from the keyboard. (In a later tutorial we may cover reading information in from text files).


The READ function tells the fortran program to record the values you enter via the keyboard and store them in variables that you define in your program. In our example triangle program we use READ like this:


In Fortran functions, any inputs needed for the function are placed inside a list within the two round-brackets (parentheses), as in READ(input1, input2, etc...). In READ, The first asterisk (*) means the input comes from the keyboard in a READ statement and goes to the screen in a WRITE statement. The second asterisk (*) means the computer decides how the I/O elements should look based on the TYPE of data in the list of variables that follow.


The list of variables that follow will be assigned the values that you type, one at a time. So in our program we have three variables (a, b, c) that we have already defined earlier on. When the program is running and gets to the READ statement, it will prompt you to enter a variable and press enter. This will happen three times, and each value you type will be assigned to a, b and c respectively.


Fortran is designed primarily for numerical calculations, and it has many built-in functions for mathematical operations. In the example triangle-area program, we use the basic functions: +, -, *, / for addition, subtraction, multiplication, and division. There is one other function that we use in the above example, SQRT(), which finds the square root of a number.


Two logical operators are used in this example: the greater-than operator > and the .AND. operator. Greater than behaves in the same way as its mathematical counterpart. E.g. 1 > 2 would give the answer FALSE, because one is not greater than two.


The .AND. operator checks to see if both expressions on either side are true or false. E.g. (1


All but the most trivial programs will have to make decisions depending on the type of data input or the results of a calculation. We can refer to this as the program flow or program logic. It is a bit like the logical decisions we make in everyday life:


Fortran uses these two keywords to make logical decisions within a program. To do something based on a condition being met, the IF [CONDITION(S)...] THEN construct is used. The condition part that follows the IF statement may be a single LOGICAL variable, or a logical expression itself (such as in our example). The THEN keyword marks the start of the code that will be executed if the condition is true.


The ELSE statement is optional, but useful if you have an action you want to perform if the condition is false. If this optional section is not provided, the program will simply move on to the next section of code without taking further action. We use an ELSE statement here to print an error message if our inputs indicate we are not dealing with a triangle.


If you have come from a background in using languages such as R, Python, or MATLAB, you may not have encountered this compilation stage before, as the usual method of running programs written in these languages hides away the compilation stage from the user. These types of languages (R, Python, etc.) are sometimes described as interpreted languages. The program is run through a special program called the interpreter, and this does all the compilation dynamically, or at run-time.

3a8082e126
Reply all
Reply to author
Forward
0 new messages