Chapter 11 MORE FLEXIBLE PROCEDURES - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

Chapter 11 MORE FLEXIBLE PROCEDURES

Description:

Chapter 11 MORE FLEXIBLE PROCEDURES. 11.1 A brief review of procedures ... to calculate trigonom. (intrinsic) functions when the angle is given in degrees , minutes, ... – PowerPoint PPT presentation

Number of Views:70
Avg rating:3.0/5.0
Slides: 27
Provided by: assis7
Category:

less

Transcript and Presenter's Notes

Title: Chapter 11 MORE FLEXIBLE PROCEDURES


1
Chapter 11 MORE FLEXIBLE PROCEDURES
  • 11.1 A brief review of procedures
  • 11.2 Procedure interfaces and interface blocks
  • 11.3 Actual and dummy arguments
  • 11.4 Saving the values and local objects on
  • exit from a procedure
  • 11.5 Recursive procedures
  • 13.6 Writing generic procedures (skip)
  • 11.7 Scope and scoping units (skip)
  • 11.8 Internal procedures (skip)

2
  • Procedures studied intrinsic (e.g. SIN) ,
    external
  • Unless external proc. is in a module (i.e. it has
    explicit interface), it has implicit interface
    i.e. calling program has
  • only info on number type of arguments
  • Intrinsic procedures have explicit interface.
  • Can make procedure interface explicit by
  • INTERFACE
  • SUBROUTINE alpha_sort(name)
  • IMPLICIT NONE
  • CHARACTER (LEN ) , DIMENSION(
    ) ,

  • INTENT(INOUT) name
  • END SUBROUTINE alpha_sort
  • END INTERFACE

3
  • This cant be used if proc. is inside a module,
    because it has explicit interface
  • Actual and dummy args
  • A call to a subroutine links the actual
    args in prog. with the subrout. args
  • Two methods
  • (1) To pass the locations of the actual
    arguments in the memory to the procedure in
    such a way as to enable the dummy arguments to
    refer to the same memory locations as did the
    actual arguments, and
  • (2) To copy the values of the actual arguments
    to variables in the procedure representing the
    dummy arguments when procedure is entered, and
    then to copy the value of the dummy arguments
    back to actual arguments when the procedure has
    finished

4
  • A dummy argument may be a scalar, array or
    procedure name
  • e.g. for function we must declare
  • REAL , EXTERNAL dummy_fun_name
  • Also in the calling prog.
  • e.g. INTEGER , EXTERNAL my_function
  • REAL , INTRINSIC SIN
  • Example (prog external intrins. funct.) to
    calculate trigonom.
  • (intrinsic) functions when the angle is given in
    degrees , minutes,
  • seconds.
  • 2? radians 360 degrees
  • ? 3.141 592 65 36
  • 1 degree 60 Mins
  • 1 Min 60 secs

5
  • Analysis use module for const.
  • MODULE Universal_Constants
  • IMPLICIT NONE
  • REAL , PARAMETER pi
    3.1415926536
  • END MODULE Universal_Constants
  • Data design
  • Purpose Type
    Name
  • A Dummy arguments
  • Degrees part of angle INTEGER
    degrees
  • Minutes part INTEGER
    minutes
  • Seconds part INTEGER
    seconds
  • B Global constant in module Universal_constants
  • ?
    REAL pi
  • C Local variable
  • Angle in radians REAL
    angle

6
  • Structure plan
  • 1 Convert angle in degrees etc. to radians
  • 2 Calculate trig_fun(angle)
  • 3 Return
  • Solution
  • REAL FUNCTION trig_fun_degrees(trig_fun, degrees,
    minutes , seconds)
  • USE Universal_Constants
  • IMPLICIT NONE
  • ! This function is a general trigonometry
    procedure for
  • ! angles in degrees, minutes and seconds
  • ! Dummy arguments
  • REAL , EXTERNAL trig_fun ! Declare
    dummy arg.
  • INTEGER , INTENT ( IN) degrees , minutes
    , seconds
  • ! Local variable
  • REAL angle
  • ! Convert angle to radius
  • angle (degrees minutes / 60.0 seconds/
    3600.0) pi/180.0

7
  • ! Use supplied intrinsic to calculate
    required function
  • trig_fun_degrees trig_fun(angle)
  • END FUNCTION trig_fun_degrees
  • PROGRAM test_for_trig_fun_degrees
  • IMPLICIT NONE ! This program is a test
    program for trig_fun_degrees
  • ! Declarations
  • REAL , INTRINSIC SIN , COS , TAN !
    Declare functions
  • REAL , EXTERNAL trig_fun_degrees ! In
    calling progr.
  • INTEGER degrees , mins , secs
  • CHARACTER answer
  • ! Loop to ask for an angle
  • DO
  • PRINT , Please give an angle in
    degrees, minutes and seconds
  • PRINT , without any fractional
    parts

8
  • PRINT , Degrees
  • READ , degrees
  • PRINT , Minutes ( 0 59)
  • READ , mins
  • PRINT , Seconds (0 59)
  • READ , secs
  • ! Calculate and display its sin ,
    cosine , and tangent
  • PRINT , Its sine is ,
    trig_fun_degrees(SIN , degrees , mins , secs)
  • PRINT , Its cosine is ,
    trig_fun_degrees(COS , degrees , mins , secs)
  • PRINT , Its tangent is ,
    trig_fun_degrees(TAN , degrees , mins , secs)
  • ! Ask if another test is required
  • PRINT Another one ? (Y/N)
  • READ answer
  • IF (answer / Y .AND. answer / y)
    EXIT
  • ! If answer was Y or y then repeat the
    loop
  • END DO
  • END PROGRAM test_for_trig_fun_degrees

9
  • Rules linking actual and dummy args
  • If the dummy argument is a scalar variable then
    the actual
  • argument must be a scalar object of the same
    type , such as
  • scalar variable , an array element , a
    character substring , a
  • constant or an expression.
  • If the dummy argument is a array then the actual
    argument must be an array of the same type.
  • If the dummy argument is a procedure, then the
    actual argument must either be the name of an
    external procedure or
  • the specific name of an intrinsic function.
  • The order of args is important but FORTRAN 90
    allows
  • flexibility or even not to use some args
    (if not needed)

10
  • e.g. SUBROUTINE keywords(first, second,
    third, fourth)
  • IMPLICIT NONE
  • INTEGER , INTENT(INOUT)
    first, second, third, fourth
  • .
  • .
  • .
  • CALL keywords(one, two, three,
    four) ! Positional
  • CALL keywords(first one,second
    two, ! Keyword in
  • third
    three, fourth four) ! same order
  • CALL keywords(third three , first
    one, ! Keyword in
  • fourth
    four, second two) ! different order
  • CALL keywords(one,fourth four,
    !Mixed positional
  • third
    three , second two) ! and keyword

11
  • Using keyword OPTIONAL allows calling subr. with
    fewer args
  • SUBROUTINE Keywords (first, second,
    third, fourth)
  • IMPLICIT NONE
  • INTEGER , INTENT(INOUT) first
  • INTEGER , INTENT(INOUT),OPTIONAL
    second,

  • third,
    fourth
  • .
  • .
  • CALL Keywords(one , two)
  • CALL Keywords(one , third
    three)
  • CALL Keywords(one, two,
    fourth four)

12
  • Example Function to calculate mean of array
    elements which are not
  • greater/less than min-value/
    max-value
  • Analysis 1) Use OPTIONAL args for min_value/
    max_value
  • 2) Use whole array operator, or
    use DO LOOP (if min_value /
  • max_value is included)
  • Data design
  • Purpose
    Type Name
  • A Dummy arguments
  • Data array
    REAL() array
  • Maximum value REAL ,
    OPTIONAL min_value
  • Minimum value REAL ,
    OPTIONAL max_value
  • B Local variables
  • Indication of upper limit LOGICAL
    maximum_check
  • Indication of lower limit LOGICAL
    minimum_check
  • Do loop variable INTEGER
    i
  • No. of elements used INTEGER
    count
  • Sum of elements used REAL
    sum_elems

13
  • Structure plan
  • 1 Set maximum_check and minimum_check to
    indicate if limits required
  • 2 Select case on presence of optional
    arguments
  • 2.1 Both absent
  • Calculate mean of whole array
  • 2.2 Otherwise
  • 2.2.1 Set sum_elems and count to
    zero
  • 2.2.2 Repeat for each element in
    array
  • 2.2.2.1 If element is
    in allowed range add it to sum_elems and
  • add 1 to
    count
  • 2.2.3 Calculate mean

14
  • Solution
  • REAL FUNCTION mean(array, min_value,
    max_value)
  • IMPLICIT NONE
  • ! This function calculates the mean of
    the elements of array ,
  • ! ignoring any elements outside the range
    specified by the two
  • ! Optional arguments
  • ! Dummy arguments
  • REAL , DIMENSION() , INTENT(IN) array
  • REAL , INTENT(IN) , OPTIONAL min_value
    , max_value
  • ! Local variables
  • LOGICAL minimum_check , maximum_check
  • INTEGER i, count 0
  • REAL sum_elems 0.0

15
  • ! Establish whether any limits are supplied
  • minimum_check PRESENT(min_value) !INTRINSIC
    fn returns TRUE
  • maximum_check PRESENT(max_value) ! if e.g.
    min_value is present
  • ! Take different actions depending on whether
    any limits are set
  • SELECT CASE (minimum_check .OR.
    maximum_check)
  • CASE (.FALSE.)
  • ! No limits use whole array processing
  • sum_elems SUM(array)
  • count SIZE(array)
  • CASE (.TRUE.)
  • ! One or both limits specified examine
    each element
  • DO i LBOUND (array , 1), UBOUND (array
    , 1)
  • ! Ignore element if below minimum
    value if specified
  • IF (minimum_check .AND.
    array(i) lt min_value) CYCLE
  • ! Ignore element if above maximum
    value if specified
  • IF (maximum_check .AND.
    array(i) gt max_value) CYCLE

16
  • ! Include this element in the
    calculation
  • sum_elems sum_elems array(i)
  • count count 1
  • END DO
  • END SELECT
  • ! Calculate mean
  • IF (count gt 0) THEN
  • mean sum_elems / count
  • ELSE
  • PRINT , No items in specified range
    ZERO returned
  • mean 0.0
  • END IF
  • END FUNCTION mean

17
  • Saving the values of local objects on exit from a
    procedure
  • Question On exit from a procedure, are local
    objects (e.g. variables, constants) saved or
    deleted?
  • Answer They are saved if
  • are initialized in the procedure
  • Using the keyword SAVE in the MODULE (e.g. see
    p.456) or in the MAIN program

18
  • SUBROUTINE new_page
  • IMPLICIT NONE
  • ! This subroutine prints a heading and the page
    number at the top of !the next page
  • ! Local variable
  • INTEGER page 0
  • ! Update page number and print heading
  • page page 1
  • PRINT (1, 20X, Example page heading, 15X,
    I3//), page
  • END SUBROUTINE new_page
  • In the Main Program
  • CALL new_page

19
  • Recursive procedures
  • When procedure calls itself we need keyword
  • RECURSIVE SUBROUTINE name( )
  • Example Use a recursive procedure to calculate
    n!
  • Analysis n! n(n-1)(n-2) .21 for n gt 0,
    0! 1
  • Another, recursive, way of expressing this is
  • for n gt 0 n! n(n-1)!
  • for n 0 n! 1

20
  • Data design
  • Purpose Type Name
  • Dummy arguments
  • The number (n) whose INTEGER n
  • factorial is required
  • n! REAL factorial_n

21
  • Structure plan
  • 1 Select case on n
  • 1.1 n 0
  • 1.1.1 factorial_n 1
  • 1.2 n gt 0
  • 1.2.1 Call factorial to calculate (n-1)!
  • 1.2.2 factorial_n n(n-1)!
  • 1.3 n lt 0
  • 1.3.1 Error return factorial_n 0

22
  • Solution
  • RECURSIVE SUBROUTINE factorial (n, factorial_n)
  • IMPLICIT NONE
  • ! Dummy arguments
  • INTEGER, INTENT(IN) n
  • REAL, INTENT(OUT) factorial_n
  • ! Determine whether further recursion is
    required
  • SELECT CASE(n)
  • CASE (0)
  • ! Recursion has reached the end
  • factorial_n 1.0
  • CASE (1)
  • ! Recursive call(s) required to obtain (n-1)!
  • CALL factorial(n-1, factorial_n)

23
  • ! Now calculate n! n(n-1)!
  • factorial_n nfactorial_n
  • CASE DEFAULT
  • ! n is negative return zero as an error
    indicator
  • factorial_n 0.0
  • END SELECT
  • END SUBROUTINE factorial
  • e.g. in Main program CALL factorial (6,
    factorial_n)
  • Then factorial_n 6!
  • Note The case statements in the above subroutine
    are similar to loop
  • factorial 1
  • DO i 1, n
  • factorial ifactorial
  • END DO
  • Note Skip recursive Bisection program example

24
  • Recursive functions
  • RECURSIVE FUNCTION fun_name( ) RESULT (res_name)
  • The type of function is declared by declaring the
    type of res_name e.g.
  • RECURSIVE FUNCTION factorial(n)
    RESULT(factorial_n)
  • IMPLICIT NONE
  • ! Result variable
  • REAL factorial_n
  • ! Dummy argument
  • INTEGER, INTENT(IN) n

25
  • ! Determine whether function recursion is
    required
  • SELECT CASE(n)
  • CASE (0)
  • ! Recursion has reached the end
  • factorial_n 1.0
  • CASE (1)
  • ! Recursive call(s) required to obtain (n-1)!
  • factorial_n nfactorial(n-1)
  • CASE DEFAULT
  • ! n is negative return zero as an error
    indicator
  • factorial_n 0.0
  • END SELECT
  • END FUNCTION factorial

26
  • Progress of execution for recursion
  • Factorial(6) 6factorial(5) 6(5
    factorial(4))
  • 6(5(4 factorial(3))) 6(5(4(3factorial(2)
    )))
  • 6(5(4(3(2factorial(1)))))
    6(5(4(3(21))))
  • 6(5(4(32))) 6(5(46)) 6(524)
    6120 720.
Write a Comment
User Comments (0)
About PowerShow.com