Lesson 9 - Learning Goals

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

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 :

- The number of arguments
- Type of arguments
- Order of 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 !!!

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

__OUTPUT :
__

THE VALUE OF 1 IS 1

THE VALUE OF 1 IS 0

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

**Conversion from real to integer ( truncation )**

INT(X) : Sign of X times the largest integer |X|

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

**Conversion from integer to real**

FLOAT(N) : The real (floating point) form of the integer N.

FLOAT(2) = 2.0

**Absolute value**

IABS(N) : Absolute value of N. (N is Integer)

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

**Remaindering**

MOD(N,K) : The remainder when N is divided by K (i.e., N

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

**Maximum and minimum**

MAX0(N1,N2,N3,…) : Maximum of two or more integer

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.

**Square root**SQRT(X) : The square root of X, where X 0.

**Exponential and logarithmic**

EXP(X) : The exponential function.

ALOG(X) : The natural logarithm, where X > 0.

ALOG10(X) : The common logarithm, where X 0.0

**Trigonometric**SIN(X) , COS(X) , TAN(X)

**Inverse trigonometric**ASIN(X) , ACOS(X) , ATAN(X) , ATAN2(Y,X)

**Hyperbolic**SINH(X) , COSH(X) , TANH(X)

Go back to lecture menu

Go back to main page