Introduction To FORTRAN
Introduction To FORTRAN
1. Programming Environment
Programming environment refer to the compiler and other
associated development tools. Since FORTRAN is a compiled
language, one would need a text editor to write the code, and a
compiler to compile the code written to a format that can be
understood by the computer. Simple text editors like Notepad to
advanced ones like Visual Studio Code can be used. For program
compilation, Open Source compilers like GFortran can be used.
Fortran files can have a variety of files extensions such as .f90, .f,
although .f90 is the most accepted standard.
3. Comments
Comments are used to add context to a program. They help explain
what the program does and they are not compiled by the compiler.
In FORTRAN, the exclamation mark signifies a comment and
anything after it is ignored by the compiler. Example:
program addNumbers
implicit none
! This shows how to use comments in FORTRAN
INTEGER :: num1, num2, result
num1 = 2
num2 = 2
result = num1 + num2
! The answer is 4, but the compiler won’t show
this write up
write(*,*) “The answer is :”, num1 + num2
end program addNumbers
The output would be:
4. Delimiters
Delimiters are special and unique character or a series of characters
that indicate the beginning or end of a specific statement, string or
function body set. In FORTRAN, the delimiter is a blank space and
multiple blank spaces are ignored. In the example above, the blank
space makes the compiler skip compilation to the next line.
5. Whitespaces
Whitespaces is any character or series of characters which creates
space on a page, but does not display a visible mark. An example
of this is tabs. For example:
program showWhiteSpace
implicit none
write(*,*) 'This contains whitespaces'
end program showWhiteSpace
The output would be:
6. Identifier
Identifiers are names given to identify different entities in a
program. In FORTRAN an identifier must satisfy the following
conditions:
It must not exceed 31 characters
The first character must be a letter
The remaining character(s) can be a letter, a number, or an
underscore. Any other value is not allowed.
Identifiers are case-insensitive. Therefore, num1 and NUM1
would refer to the same thing, although good practice
advocates for consistency.
An example of an identifier is:
program identifierExample
implicit none
INTEGER :: number !number here is an identifier
write(*,*) 'This contains whitespaces'
end program identifierExample
7. Keywords
Keywords are predefined, reserved words that have special
meaning to the computer. They cannot be used as identifier names.
FORTRAN has the following keywords:
8. Operators
An operator is a symbol that tells the compiler to perform specific
mathematical or logical manipulation. There are 3 operators in
FORTRAN, and they are:
Arithmetic: The available operators are:
9. Data type
A data type specifies the type of value of a variable and the type of
mathematical, relational or logical operation that can be applied on
the variable. FORTRAN has 5 intrinsic data types, and they are:
Integer type: This data type can hold integer values (whole
numbers that are either positive or negative).
program integerDataType
implicit none
INTEGER :: a = 2
write(*,*) a
end program integerDataType
Real type: This data type holds floating point numbers.
program realDataType
implicit none
REAL :: a = 2.9
write(*,*) a
end program realDataType
Functions
A FORTRAN function is a procedure whose result is a single
number, logical value, character string or array. A function should
not modify its arguments.
The returned quantity is known as function value, and it is
denoted by the function name. The general syntax for
defining a function is:
function name(arg1, arg2, ....)
[declarations, including those for the
arguments]
[executable statements]
end function [name]
Let’s take an example to get the square root of a number
using functions:
program numberRoot
implicit none
REAL :: number, getNumberRoot
number = getNumberRoot(10.00)
write(*,*) "The root of 10 is ", number
end program numberRoot
function getNumberRoot(num)
implicit none
REAL :: getNumberRoot
REAL :: num
getNumberRoot = sqrt(num)
end function getNumberRoot
Control structure
Control Structures are the blocks that analyze
variables and choose directions in which to go based
on given parameters. There are different basic control
structures in programming and they are:
o Loops: A loop statement allows us to execute a
statement or group of statements multiple
times. The general structure of a loop is:
Do While loop
It repeats a statement or a group of statements while a
given condition is true. It tests the condition before
executing the loop body. The general syntax is:
do while (logical expr)
statements
end do
case ('A')
write(*,*) "Excellent!"
case ('B')
case ('C')
write(*,*) "Well done"
case ('D')
write(*,*) "You passed"
case ('F')
write(*,*) "Better try again"
case default
write(*,*) "Invalid grade"
end select
I/O Operations
o Input: This allows the user to enter
information. The read statement is used
to collect user data.
program input
implicit none
INTEGER :: number
read(*,*) number
write(*,*) number
end program input
Comments
To specify comments in PHP, we // for single line
comments or #. For multi-line comments, we make use
of /* */.
Delimiters
PHP’s delimiter is the semicolon (;)
<?php
$number = 10; //The semicolon is the
delimeter
$anotherVariable = 20;
Identifiers
In PHP, identifiers begin with the $ sign followed
by the name of the identifier.
I/O Operations
The echo statement is used for printing out
variables and computational values.
Function
PHP also uses the function keyword to followed
by the name of the function. It however uses
curly braces to signify program block.
References
1.https://www.php.net/manual/en/intro-
whatis.php
2.https://www.tutorialspoint.com/fortran/
fortran_do_while_loop.htm
3.https://www.tutorialspoint.com/fortran/
nested_if_construct.htm
4.https://www.tutorialspoint.com/fortran/
select_case_construct.htm
5.https://www.tutorialspoint.com/fortran/
fortran_procedures.htm
6.https://www.tutorialspoint.com/fortran/
fortran_basic_input_output.htm