View
250
Download
2
Embed Size (px)
Citation preview
Chapter 6Programming with Functions
FUNCTIONS
Intrinsic Functions (or called library functions)
Function Subprograms: programmer-defined functions
Function Subprograms
The subprogram can be made accessible to a program, called the main program, in three ways:
Internal subprogramModule subprogramExternal subprogram
Argument Association (Figure 6.1)
Fundamental Scope PrincipleThe scope of an entity is the program or subprogram in which it is declared.Scope Rule 1: An item declared within a subprogram is not accessible outside that subprogram.Scope Rule 2: A global entity is accessible throughout the main program and in any internal subprogram in which no local entity has the same name as the global item.
Saving the Values of Local Variables
The values of local variables in subprogram are not retained from one execution of subprogram to the next unless either:
1. they are initialized in their declarations, or
2. The are declared to have the SAVE attribute.
Saving the Values of Local Variables
FUNCTION F (......) INTEGER :: Count = 0
...... ...... Count = Count + 1
...... ......END FUNCTION F (......)
or by using SAVE statement
Declaring a Function’s Type
FUNCTION Atomic_Number (X, Y)INTEGER :: Atomic_NumberREAL, INTENT (IN) :: X, Y……….. ……….. ………..……….. ……….. ………..
END FUNCTION Atomic_Number
INTEGER FUNCTION Atomic_Number (X, Y)REAL, INTENT (IN) :: X, Y……….. ……….. ………..……….. ……….. ………..
END FUNCTION Atomic_Number
Introduction to Modules ( 模組 )MODULE module-nameCONTAINS
subprogram_1subprogram_2
..
.subprogram_n
END MODULE module-name
Purpose: Packages subprogram_1, subprogram_2, ... , subprogram_n together into a library that can be used in any other program unit.
Using Module: USE Statement
Forms:USE module-nameUSE module-name, ONLY: listUSE module-name, rename-list
Examples: Figure 6.8 and Figure 6.9 USE Temperature_Library, ONLY: Fahr_to_Celsius
Compiling and Linking Programs and Modules
Compiling and Linking Programs and Modules
Compilation, in which the source program is translated to an equivalent machine-language, called an object program, which is stored in an object file.Linking, in which any references to functions contain a module are linked to their definitions in that module, creating an executable program, which is stored in an executable file.
Compiling and Linking Programs and Modules
Separate compilation of program’s source file, creating an object file.Separate compilation of the module, creating a different object file.Linking the function calls the program’s object file to function definitions in the module’s object file, creating an executable program.
External FunctionsEXTERNAL (外界的 ) SUBPROGRAMS Example: Figure 6.10
INTERFACES (界面 )Internal subprogram: explicit (明確的 ) interfaceExternal subprogram: implicit (不明確的;含蓄的 ) interfaceTo ensure that a compiler can perform the necessary consistency check, it is desirable that external subprograms have explicit interfaces.
Interface Blocks
Interface blocks can be used to provide these explicit interfaces.
INTERFACE Interface-bodyEND INTERFACE
Example: Figure 6.11
Chapter 7
Programming with Subroutines
Programming with Subprograms
Subprograms in Fortran can be either functions or subroutines.Complex problems are best solved by dividing them into simpler sub-problems and designing subprograms to solve these sub-problems.
Subroutine Subprogramsubroutine headingspecification partexecution partEND SUBROUTINE statement
Subroutine Heading :
SUBROUTINE subroutine-name (formal-argument-list)
CALL Statement:
CALL SUBROUTINE subroutine-name (actual-argument-list)
Example: Displaying an Angle in Degrees
An angular measurement in degrees, minutes, and seconds, say
100° 30' 36″ is equivalent to 100.510°
CALL PrintDegrees(NumDegrees, & NumMinutes, NumSeconds)
Figure 7.1: Displaying an Angle in Degrees
SUBROUTINE PrintDegrees (Degrees, Minutes, Seconds)
INTEGER, INTENT(IN) :: Degrees, Minutes, Seconds PRINT 10, Degrees, Minutes, Seconds, & REAL(Degrees) + REAL(Minutes)/60.0 + & R
EAL(Seconds)/3600.0 10 FORMAT (1X, I3, " degrees", I3, " minutes", I3, & "
seconds" / 1X, "is equivalent to" / 1X, F7.3, & " degrees") END SUBROUTINE PrintDegrees
Subroutine Subprograms
Subroutine subprograms have many features in common with function subprograms:They are program units designed to perform particular tasks under the control of some other program unit.The have the same basic form: each consists of a heading, a specification part, an execution part, and an END statement.They may be internal, module, or external subprogram
Subroutine SubprogramsThe different, however, in the following
respects:Functions are designed to return a single value to the program unit that references them.Subroutines often return more than one value, or they may return no value at all but simple perform some task such as displaying a list of instructions to the user.Functions return values via function names; subroutines return values via arguments.A function is referenced by using its name in an expression, where a subroutine is referenced by a CALL statement.
Example of a Subroutine That Returns Values: Converting Coordinates Figure 7.3 x = r × cos θ
y = r × sin θ
!-Convert_to_Rectangular---------------------------------------------
! Subroutine to convert polar coordinates (R, Theta) to rectangular
! coordinates (X, Y). ! ! Accepts: Polar coordinates R and Theta (in radians) ! Returns: Rectangular coordinates X and Y !--------------------------------------------------------------------
Converting Coordinates
SUBROUTINE Convert_to_Rectangular(R, Theta, X, Y)
REAL, INTENT(IN) :: R, Theta REAL, INTENT(OUT) :: X, Y X = R * COS(Theta) Y = R * SIN(Theta) END SUBROUTINE Convert_to_Rectangular
Argument Association
CALLConvert_to_Rectangular & (RCoord, TCoord, & XCoord, YCoord)
INTENT (IN) attributeINTENT (OUT) attributeINTENT (INOUT) attribut
e
7.3 Random numbers Generators
Some initial value called seed is required to begin the process of generating random numbersEach random number produced is used in computation of the next random number. Fortran 90 provides the subroutine RANDOM_SEED to initial the random number generator RANDOM_NUMBER, which is a subroutine that produces random real numbers uniformly distributed over the range 0 to 1.
Example Dice Tossing (Figure 7.6)
! Seed the random number generator CALL RANDOM_SEED
....... CALL RANDOM_NUMBER(R1) CALL RANDOM_NUMBER(R2) Die_1
= 1 + INT(6*R1) Die_2 = 1 + INT(6*R2) Pair = Die_1 + Die_2
Normal Distributions
Probabilities for Dice Tossing
7.6 Subprograms as Arguments
b
adxxf )(
B
Adxx )exp( 2
Subprograms as Arguments
In our examples of subprograms that far, the actual arguments have been constants, variables, or expressions, but Fortran also permits functions and subroutine as arguments for other subprograms. In this case, the function or subroutine must be a module subprogram, an external subprogram, or an intrinsic subprogram. Also, no INTENT attribute is used for formal argument that is a subprogram.
Module containing a function Integrand (Figures 7.11 & 7.12)MODULE Integrand_FunctionCONTAINS FUNCTION Integrand(X) REAL :: Integrand REAL, INTENT(IN) :: X Integrand = EXP(X**2) END FUNCTION IntegrandEND MODULE Integrand_Function
Module Subprograms as Arguments
PROGRAM Definite_Integral_2USE Integrand_Function IMPLICIT NONE REAL :: A, B INTEGER :: Number_of_Subintervals WRITE (*, '(1X, A)', ADVANCE = "NO") & "Enter the interval endpoints and the # of
subintervals: " READ *, A, B, Number_of_Subintervals CALL Integrate(Integrand, A, B,
Number_of_Subintervals)
External Subprograms as Arguments
type, EXTERNAL :: function-name
Figure 7.13, p. 480
Intrinsic Subprograms as Argument
Integrand = SIN (x)
Figure 7.14, p. 481
Interface Blocks
INTERFACE FUNCTION Integrand(X)
REAL :: Integrand REAL, INTENT(IN) :: X
END FUNCTION Integrand END INTERFACE
Interface Blocks (Figure 7.15, p. 483)
REAL FUNCTION Integrand(X)
REAL, INTENT(IN) :: X Integrand = EXP(X**2)
END FUNCTION Integrand
7.7 Optional and Keyword Arguments of Subprograms
A + BX + CX2 +DX3 + EX4
FUNCTION Polynomial (X, A, B, C, D, E) REAL :: Polynomial REAL, INTENT (IN) :: X, A, B, C, D, E Polynomial = A + B*X + C*X**2 & + D*X**3 + E*X**4END FUNCTION Polynomial
Keyword Arguments (Figure 7.16)
FUNCTION Polynomial(X, A, B, C, D, E) REAL :: Polynomial REAL, INTENT(IN) :: X, A REAL, INTENT(IN), OPTIONAL :: B, C, D, E Polyn
omial = A IF (PRESENT(B)) Polynomial = Polynomial + B*X IF (PRESENT(C)) Polynomial = Polynomial + C*X**2 IF (PRESENT(D)) Polynomial = Polynomial + D*X**3 IF (PRESENT(E)) Polynomial = Polynomial + E*X**4 END FUNCTION Polynomial