1 / 23

CS1001 Lecture 16

CS1001 Lecture 16. FUNCTIONS SUBROUTINES SCOPE MODULES EXTERNAL SUBPROGRAMS. Function General Form. FUNCTION function_name ( formal_argument_list ) type_identifier :: function-name (Declaration section) … (Execution section) … function_name = expr END FUNCTION function_name

kacy
Télécharger la présentation

CS1001 Lecture 16

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS1001 Lecture 16 • FUNCTIONS • SUBROUTINES • SCOPE • MODULES • EXTERNAL SUBPROGRAMS

  2. Function General Form FUNCTION function_name (formal_argument_list) type_identifier :: function-name (Declaration section) … (Execution section) … function_name = expr END FUNCTION function_name formal_argument_list is a list of identifiers (may be blank) type_identify is used to identify the type of the FUNCTION Alternate Heading: type_identifier FUNCTION function_name (formal_argument_list)

  3. Argument Passing • Arguments are used to pass information between (to/from) caller and callee. • INTENT Specifier Tells how the arguments are to transfer information • type, INTENT(IN) :: argumentfor inputs TO either a function or subroutine • type, INTENT(OUT) :: argumentfor outputs FROM a function or subroutine (but not good practice to have OUT arguments in function) • type, INTENT(INOUT) :: argument for both TO and FROM a subprogram

  4. INTENT (IN) specification • Used to ensure • value of the actual argument is passed to the formal parameter • the value of the formal argument cannot be changed while the function is being executed • Well designed Fortran function • produce a single output value from one or more input values • never modify its own input arguments -- always declare the arguments with INTENT(IN) attribute.

  5. Unintended side effects • Changing the value of a variable in one part of the program affects, unintentionally, the value of that variable or other variables in other parts of the program. • Often dangerous because • they might cause wrong results • are hard to debug (therefore, wrong results might go unnoticed.)

  6. Old FORTRAN IV Example Name value in storage Sum 0 10 10 IMAX 10, then 5 In calling program Sum = 0 Sum = addsum (10) . . . Sum = Sum + 10 Print *, Sum In Function addsum (IMAX) . . . IMAX = 5 addsum = . . . (say 20) END 1 2 Name value in storage Sum 20 10 5 3 3 5 4 1 6 Name value in storage Sum 20 10 5 4 5 2 After 5 Name value in storage Sum 25 10 5 Print Sum will give 25 At 6

  7. SUBROUTINE General Form SUBROUTINE Subroutine_name (formal_argument_list) (Declaration section) … (Execution section) … subroutine_name = expr END SUBROUTINE subroutine_name formal_argument_list is a list of identifiers (may be blank)

  8. Subroutine Reference • Each subroutine should have a header. • The program using the subroutine does not declare the subroutine. • The subroutine is referenced via a CALL statement • The code for the subroutine follows the main program CONTAINS statement and precedes the END PROGRAM statement

  9. Subroutine Example SUBROUTINE DDMMSS(Angle) REAL, INTENT(IN) :: Angle INTEGER :: Deg, Min, Sec Deg = INT(Angle) Min = INT(Angle * 60) - (Deg * 60) Sec = INT(Angle * 3600) - (Deg * & 3600) - (Min * 60) PRINT *, Angle, ‘ degrees = ‘ PRINT *, Deg, ‘ deg ‘, Min, ‘ min& & ‘, Sec, ‘ sec’ END SUBROUTINE DDMMSS

  10. Subroutine Calling • Within a program, the subroutine is called as: CALL DDMMSS(Input) • At this point in the program execution, the subroutine DDMMSS is called with angle Input as an argument, causing the subroutine to execute and display the angle in DD MM SS format

  11. Subroutine Argument Return SUBROUTINE DDMMSS(Angle, DDD, MM, SS) REAL, INTENT(IN) :: Angle INTEGER, INTENT(OUT) :: DDD, MM, SS DDD = INT(Angle) MM = INT(Angle * 60) - (DDD * 60) SS = INT(Angle * 3600) - (DDD * & 3600) - (MM * 60) END SUBROUTINE DDMMSS

  12. Subroutine Argument Return • The program would include the following statements: CALL DDMMSS(Input, Deg, Min, Sec) PRINT *, Input, ‘ degrees = ‘ PRINT *, Deg, ‘ deg ‘, Min, ‘ min ‘, Sec, ‘ sec‘ • Variables DEG, MIN, and SEC are passed to the subroutine to store the computed values for those variables Input Angle Deg DDD Min MM Sec SS Formal arguments Actual arguments

  13. Converting With Subroutine DO iRange = Init, Limit, iStep Range = iRange CALL CTOF(Range, Fahr) PRINT *, Range, Fahr END DO Where: SUBROUTINE CTOF(Cel, Far) REAL, INTENT(IN) :: Cel REAL, INTENT(OUT) :: Far Far = 1.8 * Cel + 32.0 END SUBROUTINE CTOF • Fahr = CTOF(Range) FUNCTION CTOF(Cels) REAL :: CTOF REAL, INTENT(IN) :: Cels CTOF = 1.8 * Cels + 32.0 END FUNCTION CTOF

  14. Scope • The portion of the program in which an entity (variable, constant, subprogram, types) is visible, i.e., where it is accessible and can be used. • Fundamental 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 • 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.

  15. Scope examples PROGRAM MAIN INTEGER :: X, Y : : CONTAINS SUBROUTINE SUB1(…) REAL :: Y : END SUBROUTINE SUB1 SUBROUTINE SUB2(…) INTEGER:: X : END SUBROUTINE SUB2 END PROGRAM MAIN Y of MAIN X of MAIN Y of SUB1 X of SUB2 Y of MAIN

  16. SAVE attribute • Values of local variables in a subprogram are not retained from one execution to the next • A variable in a subprogram can retain value if • initialized in the declaration • have SAVE attribute type, SAVE ::list_of_local_variables if list_of_local_variables is omitted, all local variables are saved • For example: INTEGER FUNCTION ADD(…) INTEGER :: COUNT = 0, SUM=0 INTEGER, SAVE :: RUNING_TOTAL : END FUNCTION ADD

  17. MODULES • A Module • is a program unit to package together type declarations, subprograms, and definitions of new data types • provides a way to bundle a library of useful code to be used in other program units • Form: MODULE module_name CONTAINS subprogram1 subprogram2 : type_declarations END MODULE module_name E.g., REAL, PARAMETER:: PI=3.14159

  18. Modules • Modules, once defined, can be used in a programming unit by a USE statement • For Example, PROGRAM Complex_Calc USE complex_functions : END PROGRAM Complex_Calc

  19. Preparing a Program for Execution You enter the program and save it as a source file Revised Source file Source file on disk You correct syntax error Oops! List of errors The compiler attempts to translate the program The loader places the load file into memory Good job! Other Object File New Object File Load File Executable program in memory The linker links the new object file with other object files

  20. Program source file Module source file Program Object file Module Object file Executable File Linking Programs and Modules F90 Compiler Linker F90 Compiler

  21. External Subprograms • Subprogram attached after the END PROGRAM statement • Separate and independent from main program • No access to each others local variables • Information sharing through function name and arguments • Use Interface blocks to ease compatibility checking INTERFACE Interface_body END INTERFACE

  22. Internal vs External Subprograms -Forms Internal: PROGRAM Main type :: function_name : expr = function_name (..) call subroutine_name (..) : INCLUDES type FUNCTION function_name (..) : function_name = expr1 END FUNCTION function_name SUBROUTINE subroutine_name : END SUBROUTINE subroutine_name END PROGRAM Main External: PROGRAM Main INTERFACE FUNCTION function_name (..) type :: function_name type :: arguments_list END FUNCTION function_name END INTERFACE : expr = function_name (..) : END PROGRAM Main FUNCTION function_name (..) type :: function_name type :: arguments_list : END FUNCTION function_name

  23. Module - Form Module MODULE module_name INCLUDES type FUNCTION function_name (..) : function_name = expr1 END FUNCTION function_name SUBROUTINE subroutine_name (..) : END SUBROUTINE subroutine_name : END MODULE module_name PROGRAM Main : USE MODULE module_name : expr = function_name (..) CALL subroutine_name (..) : END PROGRAM Main

More Related