Skip to content

Fortran Programming Language

Fortran Programming Language

What is Fortran?

Fortran stands for Formula Translation system. It is a general-purpose programming language, which major aim was to make the computation of things in the fields of mathematic and engineering easier. Being among the first high-level programming languages made it to be appreciated worldwide and also the fact that it is a compiled language rather than interpreted and also is portability is also a contributor to its worldwide acceptance.

Brief History of Fortran

It was developed in IBM in the year 1954 by a team of computer programmers, under the supervision of a programmer called John Backus. It was developed to be an alternative to the assembly languages. However, it was first published in the year 1957. Being a portable and easy-to-understand language, programmers began to tweak it in many ways for it to adapt to its own need, due to this fact, the language faced a constant update. However, it was later standardized by the American National Standards Association in the year 1966 and the first to be standardized was called Fortran 66.

Fortran Standards

  • Fortran 66
  • Fortran 77
  • Fortran 90
  • Fortran 95
  • Fortran 2003
  • Fortran 2008
  • Fortran 2018 (Formerly Fortran 2015)

Application of the Fortran

Fortran language is a high-level language was intended to be used for programming in the following field of studies:

  • Mathematics
  • Engineering
  • Statistics
  • Scientific computation

Fortran Compilers

These refer to platforms upon which the Fortran code is organized in other to be able to make it, a meaningful program to the user. These compilers include the following:

  • GNU Fortran
  • Intel Fortran
  • PGI Fortran

Pros of the Fortran

  • Its binaries are very fast
  • It is a high-level language
  • Good memory management
  • Good HPC support.

Data Types

There are five major data types in the Fortran programming language, though the programming language also provides room for a generic data type. The built-in data type is as follows:

  • Integer data type
  • Real data type
  • Complex data type
  • Logical data type
  • Character data type

Integer Data Type

These forms of Fortran data types can only hold integer values. Below is the syntax for declaring an integer variable:

Program Integer
    Implicit None
    Integer:: x, y, ans
    x = 2
    y = 4
    ans = x + y
    Print*, ‘ANS: ‘, ans
End Program Integer

Output:

ANS: 6

Real Data Type

These forms of Fortran data types store floating-point numbers, such as 2.099, 3.7544, 0.98, etc. there are two types of real data types; Real type (default) and double precision type. However, Fortran 90/95 provides more control over the precision of these data types by introducing the use of a kind specifier. Below is a program that outputs a real result:

Program Real
    Implicit None
    Real:: x, y, ans
    x = 2.3
    y = 5
    ans = x+y
    Print*, ‘ANS: ‘, ans
End Program Real

Output:

ANS: 7.30000019

Complex Data Type

These Fortran data types are used for storing complex numbers. A complex number has numbers two known parts: the real part and the imaginary parts. The generic Function/method ‘Cmplx()’ is used for creating a complex number. Below is a program that demonstrates the use of complex data type:

Program Complex
    Implicit None
    Integer:: i=10
    Real:: x=5.4
    Print*, ‘Complex Form: ‘, Cmplx(i,x)
End Program Complex

Output:

Complex Form: (10.0000000, 5.40000010)

Logical Data Type

This Fortran data type stores only two Boolean values either true or false. Here is an example of logical data type:

Program Logical
    Implicit None
    Logical:: x=.FALSE., y=.TRUE.
    Print*, ‘Value of x: ‘, x, ‘And Value of y: ‘, y
End Program Logical

Output:

Value of x: F And Value of y: T

Character Data Type

These Fortran data types store only characters and strings. The length of the string can be defined by using the Len specifier. If no length is specified, it is one by default. Below is a small program to demonstrate this type in action:

Program Char
    Implicit None
    Character (len=12):: n
    n=’EraInnovator’
    Print*, ‘This is ‘, n
End Program Char

Output:

This is EraInnovator

Note: In Fortran programming language, one can use variables without first declaring it, these features have led to the case of both run-time and compile-time error, that is advised, for one to start every Fortran programming process by first declaring the statement ‘Implicit none’, which makes the Fortran compiler checks your code to ensure that all variables are declared properly.

Operators

Operators are symbols that tell the compiler to perform a given mathematical or logical task. In Fortran, such operators include:

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Intrinsic Functions

Arithmetic Operators

This operator is the must use operator in all programming language includes the following symbols:

Operator Description Example
+ Addition A + B
Subtraction A – B
* Multiplication A * B
/ Division A / B
** Exponentiation A ** B
() Brackets Override’s the order
of the precedence

Note:

  • During computation, the order of precedence is strictly adhered to.
  • Where the operation is of equal precedence, it is evaluated from left to right.
  • The order of precedence can be overridden by the use of a bracket.

Relational Operators

These operators are used for deciding within a program, it makes a program to be interactive and precise. It includes the following symbols:

Operator Description Example
== Equal to A == B
/= Not Equal to A /= B
<= Less than or Equal to A <= B
< Less than A < B
>= Greater than or Equal to A >= B
> Greater than A > B

Logical Operators

This operator makes use of two logical variables or two conditional statements. These operators include the following:

Operator Description
.AND. The Result is true if both operands are true
.OR. The Result is true if either operand is true
.NOT. Logical Negation (Results true to false and vice versa)

Intrinsic Functions

In the Fortran programming language, there are rich libraries intended for in-depth mathematical computation. These libraries are referred to as intrinsic functions and it includes the following:

Function Argument Type Result Type Description
sin(x) real real Results sine of x
cos(x) real real Results cosine of x
tan(x) real real Results tangent of x
atan(x) real real Results arctangent of x
abs(x) real/integer real/integer Results absolute value of x
sqrt(x) real real Results square root of x
exp(x) real real Results e to the power of x
log(x) real real Results log of x with base 10

Control Structures

As the need for the task being executed by the computer increases, so does the need for some of this task to be done repeatedly or iteratively without the user have to manually initialize it one at a time. All programming language provides a various form of control structure statement to help in such scenarios.

Do-Loop

This loop structure ends a statement, or a series of statements, to be carried out continuously, while a given condition is true and stops once the condition becomes false. Below is a program demonstrating such events:

Program DoLoop
    Implicit None
    Integer:: i
    Do i=0, 3
        Print*, i
    End Do
End Program DoLoop

Output:

0
1
2
3

It also has other loop structures like; Do – while Loop & Nested loops.

In a big program, where there is a need for a conditional loop, Fortran provides some structure to manage such scenarios and it includes the following:

  • Exit
  • Cycle
  • Stop

Decision Statement

In computer programming, there are situations where the need to make a decision arises. In other, to handle such scenarios, all programming language comes with a decision-making structure or statement and it is no different. These structures include the following statements:

  • If… then statement
  • If… then … else statement
  • Nested if … statement
  • Select-case statement
  • Nested select case statement

The syntax for If…Then…Else the statement is as follows:

If (<conditional expression>) Then
    <Fortran Statements>
Else
    <Fortran Statements>
End If

Below is a program demonstrating the use of If…Then…Else statement:

Program IfElseExample
    Implicit None
    Integer:: x=3
    If ((x > 0) .AND. (x <= 5)) Then
        Print*, x, ‘ is Positive and less than or equal to 5.’
    Else
        Print*, x, ‘ is not desired one.’
    End If
End Program IfElseExample

Output:

3 is Positive and less than or equal to 5.

Subprograms

Within a folder containing the main Fortran program contains also important subprograms, if included in the program by the user. These important subprograms are;

  • Functions
  • Subroutines

Functions

It is a subprogram that accepts some kind of information, from that information it is expected to evaluate and return an answer or results. The programmer can generate a given function to perform a given task, they specified for it. Below is the syntax of a user-defined function:

<type> Function <name> (<arguments>)
    <declaration>
    <body of function>
    <name> = expression
End Function <name>

Here is the Example of the Function:

Program FunctionExample
    Real:: x=5, y=6, ans
    ans=getPower(x, y)
    Print*, x, ‘ to the Power of ‘, y, ‘ is ‘, ans
End Program FunctionExample

Real Function getPower(x, y)
    Implicit None
    Real:: x,y,ans
    ans=x**y
    getPower=ans
End Function getPower

Output:

5.00000000 to the Power of 6.00000000 is 15625.0000

Subroutines

It is a subprogram that accepts some kind of input information and it doesn’t return anything but instead, it can make the changes to that actual information. Below is the syntax for subroutine declaration:

Subroutine <name> (<arguments>)
    <declaration>
    <body of subroutine>
End Subroutine <name>

Here is an Example of Subroutine:

Program SubroutineExample
    Integer:: x=-5
    Print*, ‘Before calling Subroutine, x: ‘, x
    Call makeAbsolute(x)
    Print*, ‘After calling Subroutine, x: ‘, x
End Program SubroutineExample

Subroutine makeAbsolute(x)
    Implicit None
    Integer:: x
    x=abs(x)
End Subroutine makeAbsolute

Output:

Before calling Subroutine, x: -5
After calling Subroutine, x: 5

Why need Subroutines and Function in a Program?

  • It enhances code readability
  • It makes code look neat and simple
  • It makes future maintenance to be easy.

Debugging Tips:

These are ways through which one can follow in other to eliminate or handle possible errors or exceptions, that may occur within a given program or code. Here are the few tips to do this:

  • Always use ‘Implicit None’ at the beginning of a program
  • Always follow a neat coding convention
  • Add comments within your code for easy remembrance in the future
  • Do not cluster your code
  • Indent properly
  • Follow a single naming convention

Integrated Development Environment (IDE) for Fortran

These are development environments upon which Fortran code can be typed and execute from. These IDEs includes the following:

Here are some other features provided by Fortran:

  • Arrays
  • File operations
  • Formatted input and output
  • System services
  • Character string/ numeric conversion
  • Derived data types
  • Recursion

Some of the Common Keyword provided in the Fortran:

  • Print
  • Write
  • Read
  • Subroutines
  • Function
  • Kind
  • End
  • Real
  • Double precision
  • Single precision
  • Integer
  • Characters
  • Implicit none

Read: Why you should consider to write Software Applications using Python?

nv-author-image

Era Innovator

Era Innovator is a growing Technical Information Provider and a Web and App development company in India that offers clients ceaseless experience. Here you can find all the latest Tech related content which will help you in your daily needs.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.