1 / 17

CS1001 Lecture 15

CS1001 Lecture 15. EXAM1 - results SUBPROGRAM - - revisit FUNCTION. Exam 1. Average 69 Medium 72 >90 6 between 89 and 80 5 between 79 and 70 11 between 69 and 60 9 between 59 and 50 3 below 6. Function and Subroutine.

preston
Download Presentation

CS1001 Lecture 15

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 15 • EXAM1 - results • SUBPROGRAM - - revisit • FUNCTION

  2. Exam 1 • Average 69 • Medium 72 • >90 6 • between 89 and 80 5 • between 79 and 70 11 • between 69 and 60 9 • between 59 and 50 3 • below 6

  3. Function and Subroutine • Subprograms -- each is like a miniature program • Function consists of: FUNCTION heading Specification part Execution part END FUNCTION statement • Subroutine consists of: SUBROUTINE heading Specification part Execution part END SUBROUTINE statement

  4. Function • Table 6.1 (p. 324) and Appendix A list Fortran library functions • Programmer defined Functions • separate program units that perform some function(s) • included with main program • invoked at in assignment statements in the flow of the program • communicates via passing parameters and returning result • should return a single value • must be assigned a type

  5. Where to Put Function Code • The Code for programmer defined functions are inserted at the end of the main program between a CONTAINS statement and the END PROGRAM PROGRAM ProgramName . . CONTAINS Function(s) go here END PROGRAM ProgramName

  6. PROGRAM Temperature_Table IMPLICIT NONE INTEGER :: iRange, Init, Limit,Step REAL :: Range, Fahr, CTOF DO iRange = Init, Limit, Step Range = REAL (iRange) Fahr = CTOF(Range) PRINT *, Range, Fahr END DO • CONTAINS REAL FUNCTION CTOF(Cels) : : END FUNCTION CTOF END PROGRAM Temperature_Table Good practice to declare function type in any program unit that calls the function

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

  8. Converting With Function DO iRange = Init, Limit, Step Range = iRange Fahr = CTOF(Range) PRINT *, Range, Fahr END DO Where: FUNCTION CTOF(Cels) REAL :: CTOF REAL, INTENT(IN) :: Cels CTOF = 1.8 * Cels + 32.0 END FUNCTION CTOF

  9. Flow of Control PROGRAM Temperature_Table . . . Range -- actual parameter Cels -- formal parameter 1 Fahr = CTOF(Range) . . . 2 7 Range END PROGRAM Temperature_Table 3 REAL FUNCTION CTOF (Cels) . . . CTOF = 1.8 * Cels + 32.0 END FUNCTION CTOF 4 5 6

  10. Voltage example PROGRAM ComputeVoltage REAL :: Time, Volts, Voltage . . . Time = 2.5 Volts = Voltage (Time) . . . CONTAINS FUNCTION Voltage(Time) REAL :: Voltage REAL, INTENT(IN) :: Time Voltage = (Time + 0.1)* EXP(SQRT(Time)) END FUNCTION Voltage END PROGRAM ComputeVoltage Given program statement. Function Voltage is referenced with an argument of Time = 2.5 Volts is then set equal to the computed value of 12.637 Expression is evaluated as: (2.5 + 0.1) * EXP(SQRT(2.5)) = 12.637

  11. Function Equivalence • Following are the same Time = 2.5 Volts = Voltage(Time) Time = 2.5 Volts = (Time + 0.1) * EXP(SQRT(Time)) • If the computation is going to be done multiple times within a program (not within a loop), it pays to write a function. Voltage is a FUNCTION

  12. Argument (Parameter) Passing Caller ( program or subprogram) function_name (actual_argument_list) Each actual argument is associated with the corresponding formal argument -- must agree in number and type Used to pass values Callee function_name (formal_argument_list)

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

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

  15. 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.)

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

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

More Related