chapter 6 programming with functions. functions intrinsic functions (or called library functions)...

39
Chapter 6 Programming with Functions

Post on 20-Dec-2015

265 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Chapter 6 Programming with Functions. FUNCTIONS Intrinsic Functions (or called library functions) Function Subprograms: programmer-defined functions

Chapter 6Programming with Functions

Page 2: Chapter 6 Programming with Functions. FUNCTIONS Intrinsic Functions (or called library functions) Function Subprograms: programmer-defined functions

FUNCTIONS

Intrinsic Functions (or called library functions)

Function Subprograms: programmer-defined functions

Page 3: Chapter 6 Programming 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

Page 4: Chapter 6 Programming with Functions. FUNCTIONS Intrinsic Functions (or called library functions) Function Subprograms: programmer-defined functions

Argument Association (Figure 6.1)

Page 5: Chapter 6 Programming with Functions. FUNCTIONS Intrinsic Functions (or called library functions) Function Subprograms: programmer-defined functions

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.

Page 6: Chapter 6 Programming with Functions. FUNCTIONS Intrinsic Functions (or called library functions) Function Subprograms: programmer-defined functions

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.

Page 7: Chapter 6 Programming with Functions. FUNCTIONS Intrinsic Functions (or called library functions) Function Subprograms: programmer-defined functions

Saving the Values of Local Variables

FUNCTION F (......) INTEGER :: Count = 0

...... ...... Count = Count + 1

...... ......END FUNCTION F (......)

or by using SAVE statement

Page 8: Chapter 6 Programming with Functions. FUNCTIONS Intrinsic Functions (or called library functions) Function Subprograms: programmer-defined functions

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

Page 9: Chapter 6 Programming with Functions. FUNCTIONS Intrinsic Functions (or called library functions) Function Subprograms: programmer-defined functions

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.

Page 10: Chapter 6 Programming with Functions. FUNCTIONS Intrinsic Functions (or called library functions) Function Subprograms: programmer-defined functions

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

Page 11: Chapter 6 Programming with Functions. FUNCTIONS Intrinsic Functions (or called library functions) Function Subprograms: programmer-defined functions

Compiling and Linking Programs and Modules

Page 12: Chapter 6 Programming with Functions. FUNCTIONS Intrinsic Functions (or called library functions) Function Subprograms: programmer-defined functions

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.

Page 13: Chapter 6 Programming with Functions. FUNCTIONS Intrinsic Functions (or called library functions) Function Subprograms: programmer-defined functions

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.

Page 14: Chapter 6 Programming with Functions. FUNCTIONS Intrinsic Functions (or called library functions) Function Subprograms: programmer-defined functions

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.

Page 15: Chapter 6 Programming with Functions. FUNCTIONS Intrinsic Functions (or called library functions) Function Subprograms: programmer-defined functions

Interface Blocks

Interface blocks can be used to provide these explicit interfaces.

INTERFACE Interface-bodyEND INTERFACE

Example: Figure 6.11

Page 16: Chapter 6 Programming with Functions. FUNCTIONS Intrinsic Functions (or called library functions) Function Subprograms: programmer-defined functions

Chapter 7

Programming with Subroutines

Page 17: Chapter 6 Programming with Functions. FUNCTIONS Intrinsic Functions (or called library functions) Function Subprograms: programmer-defined functions

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.

Page 18: Chapter 6 Programming with Functions. FUNCTIONS Intrinsic Functions (or called library functions) Function Subprograms: programmer-defined functions

Subroutine Subprogramsubroutine headingspecification partexecution partEND SUBROUTINE statement

Subroutine Heading :

SUBROUTINE subroutine-name (formal-argument-list)

CALL Statement:

CALL SUBROUTINE subroutine-name (actual-argument-list)

Page 19: Chapter 6 Programming with Functions. FUNCTIONS Intrinsic Functions (or called library functions) Function Subprograms: programmer-defined functions

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)

Page 20: Chapter 6 Programming with Functions. FUNCTIONS Intrinsic Functions (or called library functions) Function Subprograms: programmer-defined functions

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

Page 21: Chapter 6 Programming with Functions. FUNCTIONS Intrinsic Functions (or called library functions) Function Subprograms: programmer-defined functions

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

Page 22: Chapter 6 Programming with Functions. FUNCTIONS Intrinsic Functions (or called library functions) Function Subprograms: programmer-defined functions

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.

Page 23: Chapter 6 Programming with Functions. FUNCTIONS Intrinsic Functions (or called library functions) Function Subprograms: programmer-defined functions

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 !--------------------------------------------------------------------

Page 24: Chapter 6 Programming with Functions. FUNCTIONS Intrinsic Functions (or called library functions) Function Subprograms: programmer-defined functions

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

Page 25: Chapter 6 Programming with Functions. FUNCTIONS Intrinsic Functions (or called library functions) Function Subprograms: programmer-defined functions

Argument Association

CALLConvert_to_Rectangular & (RCoord, TCoord, & XCoord, YCoord)

INTENT (IN) attributeINTENT (OUT) attributeINTENT (INOUT) attribut

e

Page 26: Chapter 6 Programming with Functions. FUNCTIONS Intrinsic Functions (or called library functions) Function Subprograms: programmer-defined functions

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.

Page 27: Chapter 6 Programming with Functions. FUNCTIONS Intrinsic Functions (or called library functions) Function Subprograms: programmer-defined functions

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

Page 28: Chapter 6 Programming with Functions. FUNCTIONS Intrinsic Functions (or called library functions) Function Subprograms: programmer-defined functions

Normal Distributions

Page 29: Chapter 6 Programming with Functions. FUNCTIONS Intrinsic Functions (or called library functions) Function Subprograms: programmer-defined functions

Probabilities for Dice Tossing

Page 30: Chapter 6 Programming with Functions. FUNCTIONS Intrinsic Functions (or called library functions) Function Subprograms: programmer-defined functions

7.6 Subprograms as Arguments

b

adxxf )(

B

Adxx )exp( 2

Page 31: Chapter 6 Programming with Functions. FUNCTIONS Intrinsic Functions (or called library functions) Function Subprograms: programmer-defined functions

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.

Page 32: Chapter 6 Programming with Functions. FUNCTIONS Intrinsic Functions (or called library functions) Function Subprograms: programmer-defined functions

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

Page 33: Chapter 6 Programming with Functions. FUNCTIONS Intrinsic Functions (or called library functions) Function Subprograms: programmer-defined functions

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)

Page 34: Chapter 6 Programming with Functions. FUNCTIONS Intrinsic Functions (or called library functions) Function Subprograms: programmer-defined functions

External Subprograms as Arguments

type, EXTERNAL :: function-name

Figure 7.13, p. 480

Page 35: Chapter 6 Programming with Functions. FUNCTIONS Intrinsic Functions (or called library functions) Function Subprograms: programmer-defined functions

Intrinsic Subprograms as Argument

Integrand = SIN (x)

Figure 7.14, p. 481

Page 36: Chapter 6 Programming with Functions. FUNCTIONS Intrinsic Functions (or called library functions) Function Subprograms: programmer-defined functions

Interface Blocks

INTERFACE FUNCTION Integrand(X)

REAL :: Integrand REAL, INTENT(IN) :: X

END FUNCTION Integrand END INTERFACE

Page 37: Chapter 6 Programming with Functions. FUNCTIONS Intrinsic Functions (or called library functions) Function Subprograms: programmer-defined functions

Interface Blocks (Figure 7.15, p. 483)

REAL FUNCTION Integrand(X)

REAL, INTENT(IN) :: X Integrand = EXP(X**2)

END FUNCTION Integrand

Page 38: Chapter 6 Programming with Functions. FUNCTIONS Intrinsic Functions (or called library functions) Function Subprograms: programmer-defined functions

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

Page 39: Chapter 6 Programming with Functions. FUNCTIONS Intrinsic Functions (or called library functions) Function Subprograms: programmer-defined functions

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