Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Functions. Type of Subprograms Fortran 90/95 allows for two types of subprograms: –Functions, and –Subroutines. In general, there are two forms of subprograms:"— Presentation transcript:

1 Functions

2 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

3 Internal subprograms Internal Subprograms –are those routines that may appear within the main program by making use of the CONTAINS statement.

4 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

5 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

6 Function Subprogram function heading specification part execution part END FUNCTION statement

7 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

8 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

9 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.

10 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

11 END FUNCTION The last statement of a function subprogram must be END FUNTION function-name

12 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

13 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

14 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.

15 Scope Principle The scope of an entity is the program or subprogram in which it is declared.

16 Scope Rule 1 An item declared within a subprogram is not accessible outside that subprogram –Such items are called local to that subprogram

17 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.

18 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

19 External subprograms –Appear in a separate program section after the main programEND statement.

20 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

21 Modules A module is a program unit used to package together type declarations, subprograms, and definitions of new data types. –MODULE name CONTAINS subprogram1 subprogram2 … END MODULE name

22 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

23 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-name USE module-name ONLY: list

24 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

25 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.

26 Compiling & Linking Source name: pgm6.f95 Module name: module6.f95 Compiling & Linking –f95 pgm6.f95 module6.f95 -o test6

27 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

28 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.

29 Interface Blocks Interface blocks can be used to provide explicit interfaces. The form needed for external subprograms is: INTERFACE Interface-body END INTERFACE

30 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.8 END FUNCTION Fahr_to_Celsius

31


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

Similar presentations


Ads by Google