UPSC IES -FORTRAN @ a Glance

FORTRAN @ Glance
Declarations Syntax:
·   The type declaration statement indicates the type and name of the variable or constant (note the two colons between the type and the name of the variable or constant). Named constants must be declared with the parameter attribute:
real, parameter:: pi = 3.1415927
·   Like named constants, variables must be declared at the beginning of a program (or subprogram) in a type declaration statement:
integer :: total
real :: average1, average2! this declares 2 real values
complex :: cx
logical :: done
character(len=80) :: line ! a string consisting of 80 characters
·      Constants can be assigned trivially to the complex number cx:
cx = (1.0, 2.0)! cx = 1.0 + 2.0i
If you need to assign variables to cx, you need to use cmplx:
cx = cmplx (1.0/2.0, -3.0) ! cx = 0.5 – 3.0i
cx = cmplx (x, y) ! cx = x + yi
The function cmplxis one of the intrinsic functions
·      Arrays
real, dimension(5) :: vector! 1-dim.real array containing 5 elements
integer, dimension (3, 3) :: matrix ! 2-dim.integer array
The individual elements of arrays can be referenced by specifying their subscripts
Ievector(1), vector(2), vector(3) are individual elements of the array “dimension”
dimension(1,1), dimension (2,3) etc

·      The array vector could also have been declared with explicit lower bounds:
real, dimension (1:5) :: vector
All the following type declaration statements are legal:
real, dimension (-10:8) :: a1! 1-dim array with 19 elements
integer, dimension (-3:3, -20:0, 1:2, 6, 2, 5:6, 2) :: grid1 ! 7-dim array
·      The number of elements of the integer array grid1 is 7 x 21 x 2 x 6 x 2 x 2 x 2 = 14112.
·      Character strings                    character (len=80) :: name
name = “Tanja”
Then
name(1:3) would yield the substring “Tan”
A single character must be referenced in a similar way:
name(2:2) yields the character “a”




If the lower subscript is omitted, it is assumed to be one, and if the upper subscript is omitted, it is supposed to be the length of the string.
Thus:
name (:3) ! yields “Tan”
name (3:) ! yields “nja”
name (:) ! yields “Tanja”

·      Fortran allows implicit typing, which means that variables do not have to be declared. If a variable is not declared, the first letter of its name determines its type: if the name of the variable starts with i, j, k, l, m, or n, it is considered to be an integer, in all other cases it is considered to be a real. However, it is good programming practice to declare all variables at the beginning of the program. The statement
implicit none

print*, “The number pi = “, pi                    ! Output print
read*, x, y, z                                                  !Input read

·      A function returns a single quantity (of any type, including array), and should, in principle, not modify any of its arguments
·      Intent
Fortran allows the specification of the “intention” with which arguments are used in the procedure:
intent (in): Arguments are only used, and not changed
intent (out): Arguments are overwritten
intent (inout): Arguments are used and overwritten
Consider the following example:
subroutineintent_example (a, b, c)
implicit none
! dummy arguments
real, intent (in) :: a
real, intent (out) :: b
real, intent (inout) :: c
b = 2 * a                          ! b modified
c = c + a * 2.0                ! sued and modified

end subroutine intent_example

Comments

Popular posts from this blog

Beauty of Mechanical Engineering

ICC Cricket World Cup 2023

NanoComposites in Tennis Balls