Академический Документы
Профессиональный Документы
Культура Документы
Intrinsic functions are some common and important functions that are
provided as a part of the Fortran language. We have already discussed some
of these functions in the Arrays, Characters and String chapters.
Numeric Functions
Mathematical Functions
Character Functions
Kind Functions
Logical Functions
Array Functions.
Numeric Functions
Sr.No Function & Description
1 ABS (A)
2 AIMAG (Z)
3 AINT (A [, KIND])
6 CMPLX (X [, Y, KIND])
9 DIM (X, Y)
Example
Live Demo
program numericFunctions
implicit none
! define constants
! define variables
real :: a, b
complex :: z
! values for a, b
a = 15.2345
b = -20.7689
z = cmplx(a, b)
When you compile and execute the above program, it produces the following
result −
abs(a): 15.2344999 abs(b): 20.7688999
aint(a): 15.0000000 aint(b): -20.0000000
ceiling(a): 16 ceiling(b): -20
floor(a): 15 floor(b): -21
z: (15.2344999, -20.7688999)
Mathematical Functions
Sr.No Function & Description
1 ACOS (X)
2 ASIN (X)
3 ATAN (X)
5 COS (X)
6 COSH (X)
7 EXP (X)
8 LOG (X)
10 SIN (X)
11 SINH (X)
12 SQRT (X)
13 TAN (X)
14 TANH (X)
Example
The following program computes the horizontal and vertical position x and y
respectively of a projectile after a time, t −
program projectileMotion
implicit none
! define constants
real :: a, t, u, x, y
a = 45.0
t = 20.0
u = 10.0
a = a * pi / 180.0
x = u * cos(a) * t
y = u * sin(a) * t - 0.5 * g * t * t
When you compile and execute the above program, it produces the following
result −
x: 141.421356 y: -1818.57861
Loops
There may be a situation, when you need to execute a block of code several
number of times. In general, statements are executed sequentially: The first
statement in a function is executed first, followed by the second, and so on.
1 do loop
2 do while loop
3 nested loops
You can use one or more loop construct inside any other loop construct.
Fortran supports the following control statements. Click the following links to
check their detail.
1 exit
If the exit statement is executed, the loop is exited, and the execution of
the program continues at the first executable statement after the end do
statement.
2 cycle
3 stop
If you wish execution of your program to stop, you can insert a stop
statement
Syntax
The general form of the do loop is −
do var = start, stop [,step]
! statement(s)
…
end do
Where,
step is the increment, if this is omitted, then the variable var is increased by unity
For example
! compute factorials
do n = 1, 10
nfact = nfact * n
end do
Flow Diagram
Here is the flow of control for the do loop construct −
The initial step is executed first, and only once. This step allows you to declare
and initialize any loop control variables. In our case, the variable var is initialised
with the value start.
Next, the condition is evaluated. If it is true, the body of the loop is executed. If
it is false, the body of the loop does not execute and flow of control jumps to the
next statement just after the loop. In our case, the condition is that the variable
var reaches its final value stop.
After the body of the loop executes, the flow of control jumps back up to the
increment statement. This statement allows you to update the loop control
variable var.
The condition is now evaluated again. If it is true, the loop executes and the
process repeats itself (body of loop, then increment step, and then again
condition). After the condition becomes false, the loop terminates.
Example 1
This example prints the numbers 11 to 20 −
Live Demo
program printNum
implicit none
! define variables
integer :: n
do n = 11, 20
print*, n
end do
When the above code is compiled and executed, it produces the following
result −
11
12
13
14
15
16
17
18
19
20
Example 2
This program calculates the factorials of numbers 1 to 10 −
Live Demo
program factorial
implicit none
! define variables
integer :: nfact = 1
integer :: n
! compute factorials
do n = 1, 10
nfact = nfact * n
! print values
end do
When the above code is compiled and executed, it produces the following
result −
1 1
2 2
3 6
4 24
5 120
6 720
7 5040
8 40320
9 362880
10 3628800
Modules
A module is like a package where you can keep your functions and
subroutines, in case you are writing a very big program, or your functions or
subroutines can be used in more than one program.
Modules provide you a way of splitting your programs between multiple files.
Defining global data that can be used by more than one routine.
Declaring variables that can be made available within any routines you choose.
Syntax of a Module
A module consists of two parts −
You can add as many modules as needed, each will be in separate files and
compiled separately.
The variables declared in a module specification part, are global to the module.
The use statement can appear in the main program, or any other subroutine or
module which uses the routines or variables declared in a particular module.
Example
The following example demonstrates the concept −
Live Demo
module constants
implicit none
contains
subroutine show_consts()
use constants
implicit none
x = 2.0
radius = 7.0
ePowerx = e ** x
area = pi * radius**2
call show_consts()
When you compile and execute the above program, it produces the following
result −
Pi = 3.14159274
e = 2.71828175
e raised to the power of 2.0 = 7.38905573
Area of a circle with radius 7.0 = 153.938049
Arrays
Arrays can store a fixed-size sequential collection of elements of the same
type. An array is used to store a collection of data, but it is often more useful
to think of an array as a collection of variables of the same type.
Declaring Arrays
Arrays are declared with the dimension attribute.
You can also declare an array with some explicit lower bound, for example −
Assigning Values
You can either assign values to individual members, like,
numbers(1) = 2.0
do i =1,5
numbers(i) = i * 2.0
end do
One-dimensional array elements can be directly assigned values using a short
hand symbol, called array constructor, like,
numbers = (/1.5, 3.2,4.5,0.9,7.2 /)
please note that there are no spaces allowed between the brackets ‘(
‘and the back slash ‘/’
Example
The following example demonstrates the concepts discussed above.
Live Demo
program arrayProg
do i=1,5
numbers(i) = i * 2.0
end do
do i = 1, 5
Print *, numbers(i)
end do
do i=1,3
do j = 1, 3
matrix(i, j) = i+j
end do
end do
do i=1,3
do j = 1, 3
Print *, matrix(i,j)
end do
end do
do i = 1, 5
Print *, numbers(i)
end do
When the above code is compiled and executed, it produces the following
result −
2.00000000
4.00000000
6.00000000
8.00000000
10.0000000
2
3
4
3
4
5
4
5
6
1.50000000
3.20000005
4.50000000
0.899999976
7.19999981
Decision
Decision making structures require that the programmer specify one or more
conditions to be evaluated or tested by the program, along with a statement
or statements to be executed, if the condition is determined to be true, and
optionally, other statements to be executed if the condition is determined to
be false.
4 nested if construct
You can use one if or else if statement inside another if or else
if statement(s).
Subroutines
In Fortran, subroutines are generally used much more frequently than functions. Functions
are expected to produce a single output variable and examples like the one just given where
an argument is modified are considered bad programming style.
Subroutines are more flexible since they can have any number of inputs and outputs. In
particular they may have not output variable. For example a subroutine might take an array
as an argument and print the array to some file on disk but not return a value to the calling
program.
Here is a version of the same program as fcn1 above, that instead uses a subroutine:
! $CLASSHG/codes/fortran/sub1.f90
1
2
program sub1
3
implicit none
4
real(kind=8) :: y,z
5
6
y = 2.
7
call fsub(y,z)
8
print *, "z = ",z
9
end program sub1
10
11
subroutine fsub(x,f)
12
implicit none
13
real(kind=8), intent(in) :: x
14
real(kind=8), intent(out) :: f
15
f = x**2
16
end subroutine fsub
17
Comments:
Now we tell the compiler that x is an input variable and y is an output variable
for the subroutine. The intentdeclarations are optional but sometimes help
the compiler optimize code.
1 ! $CLASSHG/codes/fortran/sub2.f90
2
3 program sub2
4 implicit none
5 real(kind=8), dimension(3) :: y,z
6 integer n
7
8 y = (/2., 3., 4./)
9 n = size(y)
10 call fsub(y,n,z)
11 print *, "z = ",z
12 end program sub2
13
14 subroutine fsub(x,n,f)
15 ! compute f(x) = x**2 for all elements of the array x
16 ! of length n.
17 implicit none
18 integer, intent(in) :: n
19 real(kind=8), dimension(n), intent(in) :: x
20 real(kind=8), dimension(n), intent(out) :: f
21 f = x**2
22 end subroutine fsub
This produces:
Comments:
The length of the array is also passed into the subroutine. You can avoid this
in Fortran 90 (see the next example below), but it was unavoidable in Fortran
77 and subroutines working on arrays in Fortran are often written so that the
dimensions are passed in as arguments