Functions. Type of Subprograms Fortran 90/95 allows for two types of subprograms: –Functions, and...
-
Upload
betty-bruce -
Category
Documents
-
view
226 -
download
3
Transcript of Functions. Type of Subprograms Fortran 90/95 allows for two types of subprograms: –Functions, and...
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, – External
Internal subprograms
• Internal Subprograms– are those routines that may appear within the
main program by making use of the CONTAINS statement.
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
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
Function Subprogram
• function heading
• specification part
• execution part
• END FUNCTION statement
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
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
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.
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
END FUNCTION
• The last statement of a function subprogram must be
END FUNTION function-name
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
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
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.
Scope Principle
• The 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– Such items are called local to 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.
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
External subprograms
• External subprograms– Appear in a separate program section after the
main programEND statement.
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
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
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
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
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
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.
Compiling & Linking
• Source name: pgm6.f95
• Module name: module6.f95
• Compiling & Linking– f95 pgm6.f95 module6.f95 -o test6
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
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.
Interface Blocks
• Interface blocks can be used to provide explicit interfaces.
• The form needed for external subprograms is:
INTERFACEInterface-body
END INTERFACE
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