Functions. Type of Subprograms Fortran 90/95 allows for two types of subprograms: –Functions, and...

31
Functions

Transcript of Functions. Type of Subprograms Fortran 90/95 allows for two types of subprograms: –Functions, and...

Page 1: Functions. Type of Subprograms Fortran 90/95 allows for two types of subprograms: –Functions, and –Subroutines. In general, there are two forms of subprograms:

Functions

Page 2: Functions. Type of Subprograms Fortran 90/95 allows for two types of subprograms: –Functions, and –Subroutines. In general, there are two forms of subprograms:

Type of Subprograms

• Fortran 90/95 allows for two types of subprograms: – Functions, and – Subroutines.

• In general, there are two forms of subprograms: – Internal, – External

Page 3: Functions. Type of Subprograms Fortran 90/95 allows for two types of subprograms: –Functions, and –Subroutines. In general, there are two forms of subprograms:

Internal subprograms

• Internal Subprograms– are those routines that may appear within the

main program by making use of the CONTAINS statement.

Page 4: Functions. Type of Subprograms Fortran 90/95 allows for two types of subprograms: –Functions, and –Subroutines. In general, there are two forms of subprograms:

Function Subprograms

• The Fortran language provides many intrinsic, or library functions– ABS(X), SIN(X), REAL(X)

• Fortran allows the definition of additional functions– Programmer-defined Functions, or– Function Subprograms

Page 5: Functions. Type of Subprograms Fortran 90/95 allows for two types of subprograms: –Functions, and –Subroutines. In general, there are two forms of subprograms:

PROGRAM Temperature_Conversion_1

REAL :: FahrenheitTemp, CelsiusTemp

WRITE (*, '(1X, A)') "Enter a Fahrenheit temperature: " READ *, FahrenheitTemp CelsiusTemp = Fahr_to_Celsius(FahrenheitTemp) PRINT '(1X, 2(F6.2, A))', FahrenheitTemp, & " in Fahrenheit is = ", CelsiusTemp, " in Celsius"

CONTAINS

REAL FUNCTION Fahr_to_Celsius(Temp)

REAL, INTENT(IN) :: Temp

Fahr_to_Celsius = (Temp - 32.0) / 1.8 END FUNCTION Fahr_to_Celsius

END PROGRAM Temperature_Conversion_1

Page 6: Functions. Type of Subprograms Fortran 90/95 allows for two types of subprograms: –Functions, and –Subroutines. In general, there are two forms of subprograms:

Function Subprogram

• function heading

• specification part

• execution part

• END FUNCTION statement

Page 7: Functions. Type of Subprograms Fortran 90/95 allows for two types of subprograms: –Functions, and –Subroutines. In general, there are two forms of subprograms:

Function Heading

• The function heading is a FUNCTION statement of the form:

FUNCTION function-name (argument list)– where function-name may be any legal Fortran

identifier– where argument list is an identifier or a list of

identifiers separated by commas

Page 8: Functions. Type of Subprograms Fortran 90/95 allows for two types of subprograms: –Functions, and –Subroutines. In general, there are two forms of subprograms:

Function Type

• Determining the type of FUNCTION

– Type-identifier FUNCTION function (argument list) • where type-identifier is the type of value returned by

the function

Page 9: Functions. Type of Subprograms Fortran 90/95 allows for two types of subprograms: –Functions, and –Subroutines. In general, there are two forms of subprograms:

Specification Part

• Same form as the specification part of a Fortran program with the additional stipulation that it must declare– The type of the function value– The type of each formal argument.

• These declarations should contain an INTENT specifier that tells how the arguments are to transfer the information.

Page 10: Functions. Type of Subprograms Fortran 90/95 allows for two types of subprograms: –Functions, and –Subroutines. In general, there are two forms of subprograms:

Execution Part

• Same form as the execution part of a Fortran program with the additional stipulation that it should include at least one statement that assigns a value to the identifier that names the function.

Function-name = expression

Page 11: Functions. Type of Subprograms Fortran 90/95 allows for two types of subprograms: –Functions, and –Subroutines. In general, there are two forms of subprograms:

END FUNCTION

• The last statement of a function subprogram must be

END FUNTION function-name

Page 12: Functions. Type of Subprograms Fortran 90/95 allows for two types of subprograms: –Functions, and –Subroutines. In general, there are two forms of subprograms:

PROGRAM Temperature_Conversion_1

REAL :: FahrenheitTemp, CelsiusTemp

WRITE (*, '(1X, A)') "Enter a Fahrenheit temperature: " READ *, FahrenheitTemp CelsiusTemp = Fahr_to_Celsius(FahrenheitTemp) PRINT '(1X, 2(F6.2, A))', FahrenheitTemp, & " in Fahrenheit is = ", CelsiusTemp, " in Celsius"

CONTAINS

REAL FUNCTION Fahr_to_Celsius(Temp)

REAL, INTENT(IN) :: Temp

Fahr_to_Celsius = (Temp - 32.0) / 1.8 END FUNCTION Fahr_to_Celsius

END PROGRAM Temperature_Conversion_1

Page 13: Functions. Type of Subprograms Fortran 90/95 allows for two types of subprograms: –Functions, and –Subroutines. In general, there are two forms of subprograms:

PROGRAM Temperature_Conversion_1

REAL :: FahrenheitTemp, CelsiusTemp

WRITE (*, '(1X, A)') "Enter a Fahrenheit temperature: " READ *, FahrenheitTemp CelsiusTemp = Fahr_to_Celsius(FahrenheitTemp) PRINT '(1X, 2(F6.2, A))', FahrenheitTemp, & " in Fahrenheit is = ", CelsiusTemp, " in Celsius"

CONTAINS

FUNCTION Fahr_to_Celsius(Temp) REAL:: Fahr_to_Celsius REAL, INTENT(IN) :: Temp Fahr_to_Celsius = (Temp - 32.0) / 1.8 END FUNCTION Fahr_to_Celsius

END PROGRAM Temperature_Conversion_1

Page 14: Functions. Type of Subprograms Fortran 90/95 allows for two types of subprograms: –Functions, and –Subroutines. In general, there are two forms of subprograms:

SCOPE

• The scope of an entity (variables, constants, and subprograms) is the range within a program over which that entity has meaning or is visible.

• Where an entity is accessible and can be used.

Page 15: Functions. Type of Subprograms Fortran 90/95 allows for two types of subprograms: –Functions, and –Subroutines. In general, there are two forms of subprograms:

Scope Principle

• The scope of an entity is the program or subprogram in which it is declared.

Page 16: Functions. Type of Subprograms Fortran 90/95 allows for two types of subprograms: –Functions, and –Subroutines. In general, there are two forms of subprograms:

Scope Rule 1

• An item declared within a subprogram is not accessible outside that subprogram– Such items are called local to that subprogram

Page 17: Functions. Type of Subprograms Fortran 90/95 allows for two types of subprograms: –Functions, and –Subroutines. In general, there are two forms of subprograms:

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 18: Functions. Type of Subprograms Fortran 90/95 allows for two types of subprograms: –Functions, and –Subroutines. In general, there are two forms of subprograms:

SAVE Attribute

• The values of local variables in a subprogram are not retained from one execution of the subprogram to the next unless:– They are initialized in their declarations,– They are declared to have the SAVE attribute

• type, SAVE :: list-of-local-variables

Page 19: Functions. Type of Subprograms Fortran 90/95 allows for two types of subprograms: –Functions, and –Subroutines. In general, there are two forms of subprograms:

External subprograms

• External subprograms– Appear in a separate program section after the

main programEND statement.

Page 20: Functions. Type of Subprograms Fortran 90/95 allows for two types of subprograms: –Functions, and –Subroutines. In general, there are two forms of subprograms:

PROGRAM Temperature_Conversion_3

REAL :: Fahr_to_Celsius REAL :: FahrenheitTemp, CelsiusTemp CHARACTER(1) :: Response

WRITE (*, '(1X, A)', ADVANCE = "NO") "Enter a Fahrenheit temp: " READ *, FahrenheitTemp CelsiusTemp = Fahr_to_Celsius(FahrenheitTemp) PRINT '(1X, 2(F6.2, A))', FahrenheitTemp, & " in Fahrenheit is = to ", CelsiusTemp, " in Celsius"

END PROGRAM Temperature_Conversion_3

FUNCTION Fahr_to_Celsius(Temp)

IMPLICIT NONE REAL:: Fahr_to_Celsius REAL, INTENT(IN) :: Temp

Fahr_to_Celsius = (Temp - 32.0) / 1.8

END FUNCTION Fahr_to_Celsius

Page 21: Functions. Type of Subprograms Fortran 90/95 allows for two types of subprograms: –Functions, and –Subroutines. In general, there are two forms of subprograms:

Modules

• A module is a program unit used to package together type declarations, subprograms, and definitions of new data types. – MODULE name

CONTAINSsubprogram1

subprogram2 …END MODULE name

Page 22: Functions. Type of Subprograms Fortran 90/95 allows for two types of subprograms: –Functions, and –Subroutines. In general, there are two forms of subprograms:

MODULE Temperature

CONTAINS

FUNCTION Fahr_to_Celsius(Temp)

REAL:: Fahr_to_Celsius REAL, INTENT(IN) :: Temp

Fahr_to_Celsius = (Temp - 32.0) / 1.8

END FUNCTION Fahr_to_Celsius

FUNCTION Celsius_to_Fahr(Temp)

REAL:: Celsius_to_Fahr REAL, INTENT(IN) :: Temp

Celsius_to_Fahr = 1.8 * Temp + 32.0

END FUNCTION Celsius_to_Fahr

END MODULE Temperature

Page 23: Functions. Type of Subprograms Fortran 90/95 allows for two types of subprograms: –Functions, and –Subroutines. In general, there are two forms of subprograms:

Using a Module

• Once a module has been written, its contents can be made available to any other program unit by placing in that program unit a USE statement of the form

USE module-nameUSE module-name ONLY: list

Page 24: Functions. Type of Subprograms Fortran 90/95 allows for two types of subprograms: –Functions, and –Subroutines. In general, there are two forms of subprograms:

PROGRAM Temperature_Conversion_2

USE Temperature

REAL :: FahrenheitTemp, CelsiusTemp CHARACTER(1) :: Response

DO

WRITE (*, '(1X, A)', ADVANCE = "NO") "Fahrenheit temp: " READ *, FahrenheitTemp CelsiusTemp = Fahr_to_Celsius(FahrenheitTemp) PRINT '(1X, 2(F6.2, A))', FahrenheitTemp, & " in Fahrenheit is = to ", CelsiusTemp, " in Celsius" WRITE (*, '(/ 1X, A)', ADVANCE = "NO") & "More temperatures to convert (Y or N)? " READ *, Response IF (Response /= "Y") EXIT END DO

END PROGRAM Temperature_Conversion_2

Page 25: Functions. Type of Subprograms Fortran 90/95 allows for two types of subprograms: –Functions, and –Subroutines. In general, there are two forms of subprograms:

Compiling & Linking

• Compilation– The source code is translated to an equivalent

machine language program, called object program.

• Linking– External references to a program are resolved.

• References to functions contained in a module are linked to their definitions in that module, creating an executable program.

Page 26: Functions. Type of Subprograms Fortran 90/95 allows for two types of subprograms: –Functions, and –Subroutines. In general, there are two forms of subprograms:

Compiling & Linking

• Source name: pgm6.f95

• Module name: module6.f95

• Compiling & Linking– f95 pgm6.f95 module6.f95 -o test6

Page 27: Functions. Type of Subprograms Fortran 90/95 allows for two types of subprograms: –Functions, and –Subroutines. In general, there are two forms of subprograms:

Compiling & Linking

• f95 -c pgm6.f– creates the object file

pgm6.o

• f95 -c module6.f– create the object file

module6.o

• f95 pgm6.o module6.o -o test6– Creates the executable file test6

Page 28: Functions. Type of Subprograms Fortran 90/95 allows for two types of subprograms: –Functions, and –Subroutines. In general, there are two forms of subprograms:

Interfaces

• Explicit Interface– Internal subprograms– The compiler can check the number and type of

arguments passed to a subprogram.

• Implicit Interface– External subprograms– The compiler may not be able to check whether

references to a function are correct.

Page 29: Functions. Type of Subprograms Fortran 90/95 allows for two types of subprograms: –Functions, and –Subroutines. In general, there are two forms of subprograms:

Interface Blocks

• Interface blocks can be used to provide explicit interfaces.

• The form needed for external subprograms is:

INTERFACEInterface-body

END INTERFACE

Page 30: Functions. Type of Subprograms Fortran 90/95 allows for two types of subprograms: –Functions, and –Subroutines. In general, there are two forms of subprograms:

PROGRAM Temperature_Conversion_4

INTERFACE FUNCTION Fahr_to_Celsius(Temp) REAL:: Fahr_to_Celsius REAL, INTENT(IN) :: Temp END FUNCTION Fahr_to_Celsius END INTERFACE

REAL :: FahrenheitTemp, CelsiusTemp CHARACTER(1) :: Response WRITE (*, '(1X, A)', ADVANCE = "NO") "Enter a Fahrenheit temp: " READ *, FahrenheitTemp CelsiusTemp = Fahr_to_Celsius(FahrenheitTemp) PRINT '(1X, 2(F6.2, A))', FahrenheitTemp, & " in Fahrenheit is = to ", CelsiusTemp, " in Celsius"END PROGRAM Temperature_Conversion_4

FUNCTION Fahr_to_Celsius(Temp) IMPLICIT NONE REAL:: Fahr_to_Celsius REAL, INTENT(IN) :: Temp Fahr_to_Celsius = (Temp - 32.0) / 1.8END FUNCTION Fahr_to_Celsius

Page 31: Functions. Type of Subprograms Fortran 90/95 allows for two types of subprograms: –Functions, and –Subroutines. In general, there are two forms of subprograms: