Computers in Engineering - 308-208 Lecture 9
Computers in Engineering - 308-208


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



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.

See P61.F90 and P62.F90


The arguments in the CALL and SUBROUTINE statements must be consistent with respect to :

  1. The number of arguments
  2. Type of arguments
  3. Order of arguments

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 !!!

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

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


Some FORTRAN - Supplied Functions

adapted from The ABC's of Fortran Programming

by Michael J. Merchant
  1. 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

  2. Conversion from integer to real

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

    FLOAT(2) = 2.0

  3. 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

  4. 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

  5. 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.

  6. Square root

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

  7. 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

  8. Trigonometric

    SIN(X) , COS(X) ,TAN(X)   where X is in radians

    To change from degrees to radians, divide by 180 and multiply by

    For example: Change 30o into radians

         rad = 30/180*
         rad = 
               ---
                6

  9. Inverse trigonometric

    ASIN(X) , ACOS(X), ATAN(X) , ATAN2(Y,X)

  10. Hyperbolic

    SINH(X) , COSH(X), TANH(X)


On to the next lecture
Go back to lecture menu

Copyright © 1996 McGill University. All rights reserved.