Fortran programming language is one of the oldest programming languages out there. The fact that it is still somewhat relevant in 2020 speaks volumes about its effectiveness. It is mainly used for numeric and scientific calculations, which makes it useful in scientific computations, applications, and simulations.
In this article, we will go through a Forton programming language tutorial. To follow the tutorial, you need to make sure that you have a basic understanding of computer programming languages. If you are absolutely a beginner, then the tutorial is not for you!
What is Fortran?
The Fortran programming language came into existence in 1957 by the IBM team. From the onset, it became quite popular; however, with time, its popularity decreased with the release of new, more friendly programming languages.
The key feature that makes Fortran so effective is its performance. It is optimized to run on high-performance computers, which makes it ideal for organizations that deal with financial trading, weather forecasters, and engineering simulation.
Fortran’s name came from Formula Translation, which reflects its origin in mathematics. However, it does support a variety of programming types including structured programming, modular programming, generic programming, array programming, object-oriented programming, concurrent programming, and high-performance computing.
Getting Started With Fortran
Just like any programming language, Forton also comes with a set of rules.
For instance, you need to start your Fortran program with the PROGRAM keyword. Also, the program needs to end with END PROGRAM keyword.
Your program will look like below.
PROGRAM MyFirstProgram !Write your code here END Program MyFirstProgram
In the above code, you can define your program and write functions or declare variables. It is similar to how the new high-level programming works.
Now, let’s see how variables work. Variables in any programming language are used to store values or data which is later used in the program.
To define variables in Fortran, you need to use the syntax as below.
INTEGER :: x = 1 INTEGER :: y = 3
This is similar to modern language. The above example initiates an integer variable x and y and stores 1 and 3 values to them respectively.
So, how do you store non-integer values? You do it by using the REAL keyword as below.
REAL :: x = 2.14 REAL :: pi = 3.14
Variables once stored can be used or manipulated in your program. In Fortran, the variables are not case-sensitive which makes working with variables slightly challenging. Also, the variables can be named using underscores, Latin-alphabet characters.
Alright, now let’s check out the LOGICAL type. It enables you to check variables for their TRUE or False nature.
The syntax for the LOGICAL type is as below.
LOGICAL :: Cond_1, Cond_2
This example also shows that you can initiate multiple variables in a single line.
Now, that we know how to use LOGICAL type, it is now time to learn how to read and write.
The two functions that we need to know include READ and WRITE. The READ function is used to record user inputs from the keyboard. Its syntax is as below:
READ (*,*) (input1, input2, ….)
Here the asterisk means that the inputs are coming from the keyboard.
READ (*,*) a, b, c
The WRITE function is used to showcase input to the screen.
The WRITE function is similar to READ.
WRITE (*,*) a, b, c
But what about mathematical operations? Well, Fortran is used for scientific functionality and hence it supports both basic and advanced mathematical operations including addition, subtraction, multiplication, and division.
IF, THEN & ELSE, and Logical Expressions
Fortran also lets developers use logical expressions. Once you use the LOGICAL type to define the variables, then you can define it accordingly.
Let’s see an example below.
Cond_1 = (a > 2.0) .AND. (b > 3.0) .AND. (c > 5.0)
Here, the value of Cond_1 can be either TRUE or FALSE.
Now, you can use the outcome of the logical expression and make decisions in a program.
The IF THEN ELSE can help you do just that.
IF (Cond_1) THEN WRITE (*,*) “Condition is true” ELSE WRITE(*,*) “Condition is false” END IF
Here, the IF And THEN statements are used by Fortran to run the program. If the condition is met, then the statement under it will be executed, otherwise, the statement under ELSE is executed. Also, ELSE is optional.
Moreover, you need to end the IF statement with END IF.
This leads us to the end of our Fortran programming language tutorial. The tutorial will help you get started with Fortran.
Next, you can write a simple program and test out your skill. You can also use the Fortran documentation to explore more about it.