1 / 17

Fortran: Program Units and Procedures

ICoCSIS. Fortran: Program Units and Procedures. Session Four. Outline. Main Program The stop Statement External subprograms Modules Internal subprograms Arguments of Procedures Pointer Arguments Restriction on Actual Arguments Arguments with Target Attribute

mirit
Download Presentation

Fortran: Program Units and Procedures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ICoCSIS Fortran: Program Units and Procedures Session Four

  2. Outline • Main Program • The stop Statement • External subprograms • Modules • Internal subprograms • Arguments of Procedures • Pointer Arguments • Restriction on Actual Arguments • Arguments with Target Attribute • The return Statement • Functions

  3. Introduction To break the program down into manageable units. Each such program unit corresponds to a program task that can be readily understood and, ideally, can be written, compiled, and tested in isolation. A complete program must, as a minimum, include one unit – the mainprogram. It may call subprograms (procedures): functionsor subroutines. There are various kinds of subprograms. A subprogram may be a program unit in its own right, in which case it is called an external subprogram and defines an external procedure. External procedures may also be defined by means other than Fortran. A subprogram may be a member of a collection in a program unit called a module, in which case we call it a module subprogram and it defines a module procedure. A subprogram may be placed inside a module subprogram, an external subprogram, or a main program, in which case we call it an internal subprogram and it defines an internal procedure. Internal subprograms may not be nested, that is they may not contain further subprograms.

  4. Main Program (1) • Every complete program must have one, and only one, main program. Optionally, it may contain calls to subprograms. • A main program has the following form: • [program program-name] • [specification-stmts] • [executable-stmts] • [contains internal-subprograms] • end [program [program-name]] [] are used to show optional items Shortest program: program test print *, ’Hello world!’ end program test • The only non-optional statement is the endstatement: • It signals to the compiler that it has reached the end of the program unit; • When executed, it causes the complete program to stop. Note: program program-name is optional, but by using the it the program will be more readable and by using it in the end statement specify, which exactly unit is terminated.

  5. Main Program (2) The specification statements define the environment for the executable statements: integer, real, complex, logical, character, and type(type-name) that specifies the type and other properties of the entities. The executable statements specify the actions that are to be performed as the assignment statement, the pointer assignment statement, the if statement and construct, the do and case constructs, the go to statement, and the read and print statements. The contains statement flags the presence of one or more internal subprograms. They are excluded from the sequence of executable statements of the main program. The end statement may be the target of a branch from one of the executable statements. If the end statement is executed, the program stops.

  6. The stopStatement • Another way to stop program execution is to execute a stop statement. This statement may appear in the main program or any subprogram. • In an applications where several stop statements appear in various places in a complete program, it is possible to distinguish which of the stop statements has caused the termination by adding to each one a stop code consisting of a default character constant or a string of up to five digits whose leading zeros are not significant. This might be used by a given processor to indicate the origin of the stop in a message. • stop • stop ’Incomplete data. Program terminated.’ • stop 12345

  7. External subprograms External subprograms are called from a main program or elsewhere, usually to perform a well-defined task. subroutine-stmt [specification-stmts] [executable-stmts] [contains internal-subprograms] end [subroutine [subroutine-name]] or function-stmt [specification-stmts] [executable-stmts] [contains internal-subprograms] end [function [function-name]] program game ! Main program to control a card game call shuffle ! First shuffle the cards. call deal ! Now deal them. call play ! Play the game. call display ! Display the result. end program game ! Cease execution.

  8. Modules A module provides a means of packaging global data, derived types and their associated operations, subprograms, interface blocks and namelistgroups. Everything associated with some task may be collected into a module and accessed whenever it is needed. module module-name [specification-stmts] [contains module-subprograms] end [module [module-name]] module interval_arithmetic type interval real :: lower, upper end type interval interface operator(+) module procedure add_intervals end interface : contains function add_intervals(a,b) type(interval) :: add_intervals type(interval), intent(in) :: a, b add_intervals%lower= a%lower + b%lower add_intervals%upper= a%upper + b%upper end function add_intervals : end module interval_arithmetic module state integer, dimension(52) :: cards end module state use state !has to appear at the !beginnings of the main program • Notes: • A module may contain use statements that access other modules. It must not access itself directly or indirectly. • No ordering of modules is required by the standard, but normal practice is to require each module to precede its use. • Some parts of a module can be defined as private.

  9. Internal subprograms • Internal subprograms may be defined inside main programs and external • subprograms, and within module subprograms. • They have the form • subroutine-stmt • [specification-stmts] • [executable-stmts] • end subroutine [subroutine-name] • or • function-stmt • [specification-stmts] • [executable-stmts] • end function [function-name] • Notes: • An internal subprogram automatically has access to all the host’s entities, including the ability to call its other internal subprograms. • Internal subprograms must be preceded by a contains statement in the host.

  10. Arguments of procedures (1) A procedure may access data if it is a globally defined or by passing it as an arguments: program game ! Main program to control a card game integer, dimension(52) :: cards call shuffle(cards) ! First shuffle the cards. call deal(cards) ! Now deal them. call play(cards) ! Play the game. call display(cards) ! Display the result. end program game ! Cease execution. subroutine shuffle(cards) ! Subroutine that places the values ! 1 to 52 in cards in random order. integer, dimension(52) :: cards ! Statements that fill cards : end subroutine shuffle ! Return to caller.

  11. Arguments of procedures (2) Association of the dummy arguments with the actual arguments occurring each time the call is executed. In this manner, libraries of subprograms may be built up. Being able to have different names for actual and dummy arguments provides a useful flexibility, but it should only be used when it is actually needed. When the same name can be used, the code is more readable. As the type of an actual argument and its corresponding dummy argument must agree, care must be taken when using component selection within an actual argument.

  12. Pointer arguments A dummy argument is permitted to have the attribute pointer. In this case, the actual argument must also have the attribute pointer. When the subprogram is invoked, the rank of the actual argument must match that of the dummy argument, and its pointer association status is passed to the dummy argument. On return, the actual argument normally takes its pointer association status from that of the dummy argument, but it becomes undefined if the dummy argument is associated with a target that becomes undefined when the return is executed (for example, if the target is a local variable). In the case of a module or internal procedure, the compiler knows when the dummy argument is a pointer. In the case of an external or dummy procedure, the compiler assumes that the dummy argument is not a pointer unless it is told otherwise in an interface block. A pointer actual argument is also permitted to correspond to a non-pointer dummy argument. In this case, the pointer must have a target and the target is associated with the dummy argument.

  13. Restrictions on actual arguments • Action that affects the allocation status or pointer association status of the argument or any part of it (any pointer assignment, allocation, de-allocation, or nullification) must be taken through the dummy argument. If this is done, then throughout the execution of the procedure, the argument may be referenced only through the dummy argument. • Action that affects the value of the argument or any part of it must be taken through the dummy argument unless • the dummy argument has the pointer attribute; • the part is all or part of a pointer subobject; or • the dummy argument has the target attribute, the dummy argument does not have intent in, the dummy argument is scalar or an assumed-shape array, and the actual argument is a target other than an array section with a vector subscript. • If the value of the argument or any part of it is affected through a dummy argument for which neither a), b), or c) holds, then throughout the execution of the procedure, the argument may be referenced only through that dummy argument

  14. Arguments with the target attribute In most circumstances, an implementation is permitted to make a copy of an actual argument on entry to a procedure and copy it back on return. This may be desirable on efficiency grounds, particularly when the actual argument is not held in contiguous storage. However, copy-in copy-out is not allowed when i) a dummy argument has the target attribute and is either scalar or is an assumed shapedarray; and ii) the actual argument is a target other than an array section with a vector subscript.

  15. The return statement Just as the stopstatement is an executable statement that provides an alternative means of stopping execution, so the return statement provides an alternative means of returning control from a subprogram. Ithas the form return and must not appear in a main program.

  16. Functions Functions are similar to subroutines in many respects, but they are invoked within an expression and return a value that is used within the expression. if (distance(a, c) > distance(b, c) ) then… Note the type declaration for the function result. The result behaves just like a dummy argument with intent out. It is initially undefined, but once defined it may appear in an expression and it may be redefined. The type may also be defined on the functionstatement. real function distance(p, q) or function distance(p, q) real :: distance real, intent(in), dimension(3) :: p, q distance = sqrt( (p(1)-q(1))**2 + (p(2)-q(2))**2 + (p(3)-q(3))**2 ) end function distance

  17. Numerical methods • Write your own program for solving non-linear equation. • Input: the interval (a,b), such that f(a)*f(b) < 0, and precision eps > 0 small • Find point x = (a+b)/2; • Check, whether x is a solution: abs(x) < epsif “yes” - print the result and exit the program; if “no” – assign x to either a or b to guarantee f(a)*f(b) < 0 and repeat steps 2 and 3. • Note: • Use function f(x)= log(x) as a separate function, which calculates the value of natural logarithm. • Use the interval (0.3, 2.3), and eps = 0.001.

More Related