Programming in FORTRAN95: Thomas Kalscheuer 24. January 2008
Programming in FORTRAN95: Thomas Kalscheuer 24. January 2008
Lecture III
Thomas Kalscheuer
implicit none
implicit none
implicit none
! matrix dimension
integer, parameter :: N = 10
! matrices
real, dimension(1:N,1:N) :: A, B, C
column_loop: do j = 1:N
row_loop: do i = 1:N
C(i,j) = A(i,j) + B(i,j)
end do row_loop
end do column_loop
General form of a non-deterministic loop:
[name:] do
block
if (scalar-logic-expression) exit [name]
block
if (scalar-logic-expression) cycle [name]
block
end do
[name] Properties:
• The duration of the loop is not determined beforehand. So you
need to check inside the loop whether you want to exit it
completely or perhaps cycle.
• To exit means to skip the rest of the loop completely and jump to
the first executable statement after end do.
[name] Properties:
keyword.
Arguments of subprograms:
• The dummy arguments listed in dummy-argument-list, i.e.
defined in the header of an internal subprogram, must have the
same type as the actual arguments which are used when the
internal subprogram is called from the main program.
• Dummy arguments and function names must be formally
declared in the declaration statements of the internal subprogram.
• The variable names of dummy arguments (in the internal
subprogram) and actual arguments (in the main program) may
differ.
• The result of the function is returned as function_name to the
main program.
• If a dummy argument is modified in a internal subprogram so is
its actual argument upon return from the internal subprogram
(given the argument intent permits it.)
Argument intent:
implicit none
! array dimension
integer, parameter :: n = 5
! array
real, dimension(1:n) :: x
! total sum of array elements
real :: total_sum
total_sum = total(x, n)
print *, "Sum is: ", total_sum
contains
!----------------------------------------------
function total(x, n)
! input variables
! array of real numbers
real, intent(in), dimension(:) :: x
! number of array elements
integer, intent(in) :: n
! output variables
! sum of array elements
real :: total
! local variable
! counter
integer :: i
total = 0.0
do i = 1,n
Uppsala universitet Page
3030
total = total + x(i)
end do
implicit none
! 2D vectors
real, dimension(1:2) :: x_old, x_new
! angle of rotation
real :: alpha
!----------------------------------------------
! subroutine rotate_2D_vector rotates a 2D
! vector v_old by a given angle beta. The
! result is returned in v_new.
subroutine rotate_2D_vector(v_old, beta, v_new)
! input variables
! vector to be rotated
real, intent(in), dimension(1:2) :: v_old
! angle of rotation
real, intent(in) :: beta
! output variables
! rotated vector
real, intent(out), dimension(1:2) :: v_new
! local variables
! constant pi
real, parameter :: pi=3.14159265358979323846