Fortran is an imperative programming language compiled for general purposes that are particularly suitable for numerical computing and scientific computing.
Fortran was first launched in 1954, Fortran is and is still commonly used as the oldest programming language. Scientific fields, in particular, numerical weather prediction, computational fluid dynamics, and computational physics find their applications.
Fortran is quite common in high-performance computing and is used to mark the program bench and rank the fastest supercomputers in the world.
Fortran is a compiled language, or more specifically it is compiled ahead-of-time. In other words, you must perform a special step called the compilation of your written code before you are able to run it on a computer. This is where Fortran differs from interpreted languages such as Python and R which run through an interpreter which executes the instructions directly but at the cost of computing speed.
Why Learn Fortran?
Fortran is the dominant programming language used in engineering applications. It is therefore important for engineering graduates to be able to read and modify Fortran code. From time to time, so-called experts predict that Fortran will rapidly fade in popularity and soon become extinct. These predictions have always failed. Fortran is the most enduring computer programming language in history. One of the main reasons Fortran has survived and will survive is software inertia.
Over the years, a vast collection of code has been written with Fortran in many scientific and engineering machines, allowing the language to persist.
Some of the reasons you should learn Fortran:
1. Built-in support for arguments in subroutines
2. Rich set of intrinsic functions
3. Built-in support for complex numbers
4. Support for array notation allowing operations on array sections
5. Strong aliasing rules for memory pointers, resulting in more efficient code after compilation
6. Some legacy code are written in Fortran and still in operation.
FORTRAN was designed for scientists, engineers, and programmers. There are a set of rules governing Fortran code and whether a code is valid and can be compiled. Fortran uses a popular code structure term called Syntax- a combination of characters and reserved keywords to create a Fortran program and issue an instruction to the computer.
Below are basics of Fortran Programming:
This is the structure of the Fortran program and it has a reserved keyword for doing just that. Fortran uses the
PROGRAM keyword, followed, optionally, by a name for the program to begin a program and end it with the
END PROGRAM keyword. Example:
! Do some stuff, other code, statements, functions
Within the PROGRAM statements, you can define functions, declare variables to be used in these functions, just like in other programming languages.
These are values or data used in your program. A variable can refer to a single value like 1 or point to a larger data structure such as a list. They can also be used to store data like texts, words, decimal and many more.
Each variable usually refers to a type of data. A data type describes what kind of data the variable is expected to hold. It can either numbers which are integers or words which is a string. We could say
x=3, this means
x is holding an integer type of data, however, we didn’t specify what type of data it's holding. Most programming languages require you to specify what type of data a particular variable is expected to hold.
In Fortran, variables are declared as below:
INTEGER :: n = 3
INTEGER :: m = 6
The above code in Fortran means we are creating two
INTEGER variables. The keyword
INTEGER is used to specify what kind of data we are creating and that’s a variable with numbers. There are others like
LOGICAL , etc.
Read and Write Message
This is the easiest way to see the results of a calculation or print a message on the console and also collects inputs from users to be used in a calculation for instance. This basically provides a way to print a message on the console and a way for users to type in values using the keyboard right from the program.
Read: The READ function tells the FORTRAN program to record the values you enter using the keyboard and store them in variables that you define in your program. We can see a working example :
READ(*,*) a, b, c
In Fortran functions, any inputs needed for the function are placed inside a list within the two round-brackets (parentheses), as in reading (input1, input2, etc…). When the program is running and gets to the READ statement, it will prompt you to enter a variable and press enter.
Write: WRITE function is similar to the READ function. It just prints out the given values to the console screen. For instance:
WRITE(*,*) a, b, c
This would print the values assigned to the variables specified in the code.
Fortran is primarily designed for numerical calculations, and it has many built-in functions for mathematical operations. In Fortran, we use the basic functions: +, -, *, / for addition, subtraction, multiplication, and division.
This is the logic flow of a program. This is for instance where most calculations are being made and decides on the final results. It is a bit like the logical decisions we make in our everyday life. For instance, we could say
IF its raining
THEN don’t go outside,
ELSE there is an umbrella you are free to go to. Below is an example:
LOGICAL :: Raining
LOGICAL :: UmbrellaIF (Raining .AND. Umbrella) THEN
WRITE(*,*) "Its raining and I have an umbrella, I can go out"
WRITE(*,*) "Its raining and I have no umberella, I can't g out"
Components of Decision Making:
- The IF and THEN statements: These two keywords are used to make logical decisions within a program. The
IF [CONDITION(S)…] THENconstruct is used to do something based on a condition being met.
- The ELSE statement: The ELSE statement is usually optional, although it's useful if you have something you want to do if the condition is false. Most times, the ELSE statement is used here to print an error message if the conditions are not met.
- End IF statement: This indicates the end of decision-making block. You must remember to always end it with the END IF statement.
Most programming languages usually need to be compiled or go through a process before it can be run or executed. Fortran programs need to be compiled to what the computer understands using the
gfortran compiler. This compiles Fortran code to what the computer understands. Save your code in a text file with
.f90 extension and run it against the compiler to generate the machine code.
You can run the command below in your terminal directory:
gfortran example.f90 -o example
gfortran to take the file
example.f90 and generate a program called
example for us to run. The -o flag lets us indicate an output name for our generated program. This will generate a file
example in your directory which can be run from the command line.
Advantages of Fortran
Fortran has many advantages, some of which are:
- In science circles, Fortran is very powerful. It has a wealthy tool ecosystem for this domain.
- Fortran produces the world’s fastest native code. That’s why scientific computing is perfect.
- Fortran is extremely optimized for vectorization, making it great for modeling supercomputers.
- Modern Fortran is amazingly simple to read and comprehend. Compare to C++.
- Fortran does not use the terrible syntax of the off-side rule you find in Python.
Main Features of Fortran
- FORTRAN II endorsed the language of procedural programming that enables algorithmic writing of the code.
- The COMMON declaration is used to access the global variables to be used in the programs for subroutines.
- FORTRAN II enables the subroutines to perform the tasks and the return values to be written.
- Any inherent or derived type may be a POINTER.
- Lower case characters in the source text are now permitted. A semicolon can be used in a single source line to distinguish various statements.