9.1 What do we mean by "Divide and Conquer"
9.2 What are the different kinds of SubPrograms
9.3 How to set up a Subroutine
SUBROUTINE
A SUBROUTINE is a separate
section of program that performs an operation, usually involving
some data supplied by the calling program. To demonstrate how
a subroutine is setup in FORTRAN, we will consider a simple program
to read in two numbers, swap or exchange the two values and then
print them out.
The arguments in the CALL and
SUBROUTINE statements must be consistent with respect to :
DUMMY ARGUMENTS
This term is used to describe
the arguments in the SUBROUTINE. Let say we have the arrays UNITS
and SALES. They really only exist in the main program, where they
are allocated and take up main memory space. They do not exist
in the SUBROUTINE. The subroutine is passed the address or
starting point of the array. The data is read into the arrays
in the main program and the subroutine refers to and uses this
data without having to make a copy of it into the subroutine memory
area.
Moreover, you can use a variable
name in the SUBROUTINE that you already use in the main program
because those variables do not exist in main memory. Thus, they
won't change the value of the variable of the main program even
if they have the same variable name.
For example, see program P67.F90
YOU CAN CHANGE CONSTANTS
SO BE VERY CAREFUL !!!
OUTPUT :
FUNCTION STATEMENT
Another type of subprogram is
a FUNCTION which returns a single value, and rather than using
a CALL statement, it is used in an assignment statement like the
SIN or SQRT built-in functions.
For example, see program P68.F90
INT(X) : Sign of X times the
largest integer |X|
FLOAT(N) : The real (floating
point) form of the integer N.
IABS(N) : Absolute value of N.
(N is Integer)
MOD(N,K) : The remainder when
N is divided by K (i.e., N
MAX0(N1,N2,N3,
) : Maximum
of two or more integer
EXP(X) : The exponential function.
To change from degrees to radians, divide by 180 and multiply by
For example: Change 30o into radians
On to the next lecture
PROGRAM CHANGE
!
INTERFACE
SUBROUTINE DECREM(N)
IMPLICIT NONE
INTEGER, INTENT(IN OUT) :: N
END SUBROUTINE DECREM
END INTERFACE
!
PRINT 5,1
5 FORMAT (' THE VALUE OF 1 IS ',I2)
CALL DECREM(1)
PRINT 5,1
END PROGRAM CHANGE
!
SUBROUTINE DECREM(N)
IMPLICIT NONE
INTEGER, INTENT(IN OUT) :: N
N = N-1
RETURN
END SUBROUTINE DECREM
THE VALUE OF 1 IS 1
THE VALUE OF 1 IS 0
This converts the expression to integer form.
INT(3.2) = 3
INT(-3.2) = -3
AINT(X) : Sign of times the largest integer |X|
Same as INT, but returns a real number.
AINT(3.2) = 3.0
AINT(-3.2) = -3.0
FLOAT(2) = 2.0
IABS(N) = { N, if N 0 and -N, if N < 0. }
IABS(-3) = 3
ABS(X) : Absolute value of X. (X is Real)
ABS(X) = { X, if X 0 and -X, if X < 0. }
ABS(-3.2) = 3.2
modulo K).
The function is defined as N - INT(N/K) * K. (for integer
numbers)
MOD(17,5) = 2
MOD(8,2) = 0
MOD(-5,3) = -2
AMOD(X,A) : The remainder when X is divided by A.
Similar to MOD, but for real numbers.
The function is defined as X - AINT(X/A) * A.
MOD(3.123,1.0) = 0.123
MOD(8.0,2.0) = 0.0
MOD(-7.25,2.0) = -1.25
arguments.
MAX1(X1,X2,X3,
) : Maximum of two or more real arguments.
Result is converted to integer (truncated).
AMAX0(N1,N2,N3,
) : Maximum of two or more integer
arguments. Result is converted to real.
AMAX1(X1,X2,X3,
) : Maximum of two or more real arguments.
MIN0(N1,N2,N3,
) : Minimum of two or more integer
arguments.
MIN1(X1,X2,X3,
) : Minimum of two or more real arguments.
Result is converted to integer (truncated).
AMIN0(N1,N2,N3,
) : Minimum of two or more integer
arguments. Result is converted to real.
AMIN1(X1,X2,X3,
) : Minimum of two or more real arguments.
ALOG(X) : The natural logarithm, where X > 0.
ALOG10(X) : The common logarithm, where X > 0.0
rad = 30/180*
rad =
---
6
Go back to lecture menu