Basic_comphy_ch5

Basic_comphy_ch5 - Ch. 5 Fortran 90: Subprograms...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
    1 Ch. 5 Fortran 90: Subprograms References: 1. T.M.R. Ellis et al., “ Fortran 90 programming” , Chs. 4, 11 2. L. Nyhoff & S. Leestma, “ Introduction to FORTRAN 90” , Chs. 6, 7
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
    2 Outline 5.1 Introduction 5.2 External functions 5.3 Interface blocks 5.4 Subroutines 5.5 Arrays and subprograms 5.6 Modules 5.7 More flexible subprograms (optional) (Sec. 11.5 & 11.8 of Ellis et al.)
Background image of page 2
    3 5.1 Introduction * So far, the problems that we have worked on are quite straightforward, and hence the Fortran programs are simple and small. * For complex problems, it is helpful to divide the problem into a number of simpler problems, which are then considered individually. Example: In solving the motion of a system of particles, what would be the basic components of the program? Initial data setting Time evolution Data analysis Data output Split into subprograms Initial data setting Time evolution Data analysis Data output (easier to develop and test) * In Fortran, there are 2 kinds of subprograms: Functions & Subroutines * The subprograms are controlled by either the main program or some other subprograms.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
    4 Complex problem Main program unit  subprogram 1  subprogram 2 .... Subprogram 1 Subprogram 2 .... * There may be any number of subprogram units in a complete program. * One program unit need not aware of the internal details of any other program unit. * The link between one program unit and a subsidiary unit is through the interface of the subsidiary unit (which consists of the name of the program unit and other public entities of the program unit)
Background image of page 4
    5 5.2 External functions * We have already used some Fortran intrinsic functions (e.g, SIN , LOG ,...) * However, it is often necessary to develop our own functions (called external functions ) * They are written as function subprograms: Type FUNCTION function_name ( argument_list ) “Body-of-the-function-subprogram” END FUNCTION function_name Type = REAL, INTEGER,. ..etc argument_list = d1, d2, d3,. .. Dummy arguments * A function takes values from its argument, and returns a single value via the function name.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
    6 Example: PROGRAM Main_program IMPLICIT NONE REAL :: a, b, x REAL, EXTERNAL :: cube a = 2.0 ; b = 3.0 x = b* cube ( a ) WRITE(*,*) x END PROGRAM Main_program !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ! External function cube !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! REAL FUNCTION cube ( x ) IMPLICIT NONE REAL, INTENT ( IN ) :: x cube = x**3 END FUNCTION cube x is the dummy argument (Note: this is not the “ x ” used in the main program) The function must be declared in the calling program a is the acutal argument EXTERNAL : informs the compiler that cube is an external function (This is an optional attribute) INTENT ( IN ) : informs the compiler that the dummy argument x may not be changed by the function
Background image of page 6
    7 Example: (External function with two arguments) PROGRAM Main_program IMPLICIT NONE REAL :: x, y, u, v REAL, EXTERNAL ::
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/21/2010 for the course PHYSICS 2351 taught by Professor Drlinlapming during the Spring '10 term at CUHK.

Page1 / 46

Basic_comphy_ch5 - Ch. 5 Fortran 90: Subprograms...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online