Fortran 90 Subprograms - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

Fortran 90 Subprograms

Description:

User-provided subprogram that typically performs a well-defined task within the program. ... LOG(X) returns loge of X. LOG10(X) returns log10 of X. EXP(X) returns eX. ... – PowerPoint PPT presentation

Number of Views:219
Avg rating:3.0/5.0
Slides: 27
Provided by: lenfr
Category:

less

Transcript and Presenter's Notes

Title: Fortran 90 Subprograms


1
Fortran 90 Subprograms
  • Two types of subprograms/procedures
  • Intrinsic subprograms (subroutines or functions)
  • Supplied as part of the Fortran language.
  • External (user-supplied) subprograms (subroutines
    or functions)
  • User-provided subprogram that typically performs
    a well-defined task within the program.

2
Intrinsic Subprograms
  • Mainly intrinsic functions.
  • Provided as part of the language itself.
  • Because they are implemented by the vendors they
    are usually very efficient.
  • Reflect the scientific orientation of the
    language.

3
Intrinsic Functions
  • Mathematical Functions
  • ABS(A) returns absolute value of A of same
    type an kind as A.
  • if A is complex, returns
  • COS(X) returns cosine of X.
  • SIN(X) returns sine of X.
  • TAN(X) returns tangent of X.
  • ACOS(X) returns inverse cosine of X.
  • COSH(X) returns hyperbolic cosine of X.
  • sin(x) calculates the value of sin x where x is
    in radians.
  • log(x) calculates the natural logarithm of x.
  • sqrt(x) calculates the square root of x.
  • Many intrinsic functions have a generic form
    returning a result of the same type as the actual
    argument.
  • For example,
  • log(x) returns a value of type REAL (KINDipr)
    assuming that x is declared as
  • REAL (KINDipr) x

4
Intrinsic Functions
  • SQRT(X) returns square root of X.
  • LOG(X) returns loge of X.
  • LOG10(X) returns log10 of X.
  • EXP(X) returns eX.
  • CEILING(X) returns smallest integer greater
    than or equal to X.
  • FLOOR(X) returns largest integer less than
    or equal to X.
  • etc., etc. Lots of other intrinsic functions.

5
External Procedures
  • Procedures usually supplied by users (or by
    library providers) rather than part of the
    language.
  • Top-down program design results in the
    decomposition of an algorithm into clearly
    defined sub-tasks (procedures), each of which can
    be coded, verified and tested independently
    structured code, rather than monolithic code.

6
External Procedures
  • Using well-designed procedures results in the
    following benefits
  • Independent testing of sub-tasks.
  • Reusable code sub-tasks are often required in
    many different parts of a program, or even in
    different programs. A well-tested, efficient
    procedure may be re-used many times e.g.,
    sorting, triangular factorisation, etc.

7
External Procedures
  • Isolation from unintended side-effects
    communication between the invoking main program
    and the procedure is through an argument list.
    The only main program variables that can be
    changed by the procedure are (a specified subset
    of) those in the argument list. This results in
    much easier code maintenance.

8
External Procedures
  • Two kinds of external procedures
  • Subroutines invoked by a CALL statement and can
    return multiple results through the calling
    arguments.
  • Functions invoked by naming them in an
    expression and return a single value that is used
    in the expression like an intrinsic function.

9
Subroutines
  • To call a subroutine, the main program, or
    another subroutine (but not the subroutine itself
    see later for recursive subroutines) uses a
    CALL statement
  • CALL subroutine_name (argument_list)
  • where the order and types of the actual
    arguments in the argument list must match the
    order and types of the dummy arguments declared
    in the subroutine.

10
Subroutines
  • Receives its input values and returns its
    results through an argument list.
  • SUBROUTINE subroutine_name (argument_list)
  • IMPLICIT NONE
  • ...
  • (Declaration section)
  • ...
  • (Execution section)
  • RETURN
  • END SUBROUTINE subroutine_name
  • ... indicates that subroutine_name is optional
    here.

11
Subroutines
  • The argument list contains a list of the
    variables (and/or arrays) that are being passed
    from the calling program to the subroutine
    known as dummy arguments, since the subroutine
    does not allocated any memory for them they are
    placeholders for the actual arguments that will
    be passed from the calling program when the
    subroutine is invoked.
  • Subroutine has a declaration section and an
    execution section.
  • When a program calls a subroutine, the execution
    of the calling program is suspended and the
    execution section of the subroutine is run.

12
Subroutines
  • SUBROUTINE calculate_hypotenuse (side_1, side_2,
    hypotenuse)
  • IMPLICIT NONE
  • ! Subroutine arguments
  • REAL, INTENT(IN) side_1, side_2
  • REAL, INTENT(OUT) hypotenuse
  • ! Local variables
  • REAL temp
  • ! Calculate hypotenuse
  • temp side_12 side_22
  • hypotenuse SQRT(temp)
  • RETURN
  • END SUBROUTINE calculate_hypotenuse

13
Subroutines
  • calculate_hypotenuse has three arguments in its
    dummy argument list
  • side_1, side_2 are used to pass data to the
    subroutine, but are not changed in the
    subroutine. They are declared to be input values
    with the INTENT(IN) attribute.
  • hypotenuse is set in the subroutine and its
    value is passed back to the calling progam. It is
    declared to be an output value with the
    INTENT(OUT) attribute.

14
Subroutines
  • temp is declared and used within the subroutine
    and is not accessible to any calling program. It
    is known as a local variable.
  • The RETURN statement is optional, execution
    automatically returns to the calling programme
    when the END SUBROUTINE statement is reached.
  • The subroutine can be tested independently by
    writing a test driver program. This is a small
    program that calls the subroutine with a sample
    data set.

15
The INTENT Attribute
  • It is good practice for every dummy argument to
    have an INTENT attribute associated with it.
  • The attribute can take one of three forms
  • INTENT(IN) Dummy argument is used only to pass
    data to the subroutine not changed in the
    subroutine.
  • INTENT(OUT) Dummy argument is used only to return
    results from the subroutine.
  • INTENT(INOUT) Dummy argument is used both to pass
    data to the subroutine and to return results.

16
Argument Passing
  • Argument passing in Fortran uses a
    pass-by-reference scheme.
  • When a subroutine is called, the main program
    passes pointers to the locations in memory of
    each argument in the actual argument list (not
    the actual values of the arguments).

17
Argument Passing
  • A potential pitfall with a pass-by-reference
    scheme is that the programme can continue to
    execute even if there is a mismatch between
    actual and dummy arguments this is one of the
    most common Fortran errors.
  • The programmer must ensure that the values in the
    calling argument list match the subroutines
    calling parameters in number, type (and order).

18
Simple Example
  • SUBROUTINE calculate_hypotenuse (side_1, side_2,
    hypotenuse)
  • IMPLICIT NONE
  • ! Subroutine arguments
  • INTEGER, PARAMETER isp KIND(1.0)
  • REAL (KINDisp), INTENT(IN) side_1,
    side_2
  • REAL (KINDisp), INTENT(OUT) hypotenuse
  • ! Local variables
  • REAL (KINDisp) temp
  • ! Calculate hypotenuse
  • temp side_12 side_22
  • hypotenuse SQRT(temp)
  • RETURN
  • END SUBROUTINE calculate_hypotenuse

19
Simple Example
  • PROGRAM right_angled_triangle
  • IMPLICIT NONE
  • INTEGER, PARAMETER isp KIND(1.0)
  • REAL (KINDisp) a, b, c ! sides of
    right angled triangle
  • !
  • PRINT , 'Input shorter two sides of right
    angled triangle'
  • READ , a,b
  • !
  • ! Determine and print hypotenuse
  • !
  • CALL calculate_hypotenuse (a,b,c)
  • PRINT , 'Hypotenuse is', c
  • STOP
  • END PROGRAM right_angled_triangle

20
Array Arguments
  • Another important topic is the passing of arrays
    to subroutines.
  • We will return to this later.

21
External Functions
  • A function is a procedure whose result is a
    single number (or a single array an
    array-valued function).
  • Weve already seen intrinsic functions now
    consider external, or user-defined, functions.

22
External Functions
  • Receives its input values through an argument
    list.
  • FUNCTION name (argument_list)
  • IMPLICIT NONE
  • ...
  • (Declaration section must declare the type
    of name)
  • ...
  • (Execution section)
  • ...
  • name expr
  • RETURN
  • END FUNCTION name
  • ... indicates that name is optional here.

23
External Functions
  • A function is invoked by naming it in an
    expression when invoked execution starts at
    head of function and proceeds to either the
    RETURN or END FUNCTION statement. When the
    function returns, the returned value is used to
    continue the evaluation of the expression that
    called the function.
  • The name of the function must appear on the left
    hand side of at least one assignment statement in
    the function.

24
External Functions
  • Functions use the same argument passing by
    reference scheme that subroutines use. Thus it
    is possible for a function to modify its own
    input arguments (the values in its argument list)
    these are known as side effects, and should be
    avoided.
  • To ensure that a function cannot accidently
    modify its input arguments, all arguments should
    be declared with the INTENT(IN) attribute.

25
Simple Example
  • FUNCTION hypotenuse (side_1, side_2)
  • IMPLICIT NONE
  • ! Declarations
  • INTEGER, PARAMETER isp KIND(1.0)
  • REAL (KINDisp) hypotenuse ! type of
    hypotenuse
  • REAL (KINDisp), INTENT(IN) side_1,
    side_2
  • ! Local variables
  • REAL (KINDisp) temp
  • ! Calculate hypotenuse
  • temp side_12 side_22
  • hypotenuse SQRT(temp)
  • RETURN
  • END FUNCTION hypotenuse

26
Simple Example
  • PROGRAM right_angled_triangle
  • IMPLICIT NONE
  • INTEGER, PARAMETER isp KIND(1.0)
  • REAL (KINDisp) a, b ! sides of right
    angled triangle
  • REAL (KINDisp) hypotenuse ! type of
    function
  • !
  • PRINT , 'Input shorter two sides of right
    angled triangle'
  • READ , a,b
  • !
  • ! Determine and print hypotenuse
  • !
  • PRINT , 'Hypotenuse is', hypotenuse (a,b)
  • STOP
  • END PROGRAM right_angled_triangle
Write a Comment
User Comments (0)
About PowerShow.com