Fortran Lang Intro
Fortran Lang Intro
Introduction
OCN-3205
(Computer Programming for Oceanographers)
Lecture 07
program program_name
implicit none
! type declaration statements
! executable statements
end program program_name
First Fortran Program
FORTRAN Compiler Link: https://www.tutorialspoint.com/compile_fortran_online.php
Output will be
I am a student of BSMRMU
A Simple Program in Fortran
FORTRAN Compiler Link: https://www.tutorialspoint.com/compile_fortran_online.php
Let’s write a program that adds two numbers and prints the result −
program addition
! This simple program adds two numbers implicit none
! Type declarations
real :: a, b, result
! Executable statements
a = 13.0
b = 16.0
result = a + b
print *, a,'+',b, '=', result
end program addition
When you compile and execute the above program, it produces
the following result −
13.0000000 + 16.0000000 = 29.0000000
Fortran – Integer Data Types
● Fortran provides five intrinsic data types, however, you can derive
your own data types as well. The five intrinsic types are −
●1. Integer type 2. Real type 3. Complex type
●4. Logical type 5. Character type
program testingInt
implicit none
integer(kind = 2) :: shortval ! 2- byte integer
integer(kind = 4) :: longval ! 4- byte integer
integer(kind = 8) :: verylongval ! 8- byte integer
integer(kind = 16) :: veryverylongval ! 16- byte integer
integer :: defval ! default integer
print *, huge(shortval) When you compile and execute the above
print *, huge(longval) program, it produces the following result −
print *, huge(verylongval) 32767
2147483647
print *, huge(defval) 9223372036854775807
end program testingInt 170141183460469231731687303715884105727
2147483647
Fortran - Real Data Type
Real type stores the floating point numbers, such as 2.0, 3.1415, -
100.876, etc.
Traditionally there are two different real types, the default real type
and double precision type.
program division
implicit none
real :: p, q, realRes ! Define real variables
integer :: i, j, intRes ! Define integer variables
p = 2.0 ! Assigning values
q = 3.0
i=2
j=3
realRes = p/q ! floating point division
intRes = i/j When you compile and execute the above
print *, realRes program it produces the following result −
print *, intRes 0.666666687
0
end program division
Implicit Typing
➢ Older versions of Fortran allowed a feature called
implicit typing, i.e., you do not have to declare the
variables before use. If a variable is not declared, then
the first letter of its name will determine its type.
➢ implicit none
➢ This statement turns off implicit typing.
Character Type
➢ The character type stores characters and strings.
The length of the string can be specified by len
specifier. If no length is specified, it is 1.
➢ For example,
➢ character (len = 40) :: name
➢ name = “Bangabandhu University”