CS 1001 Lecture 13 PRACTICE EXAM revisit SUBPROGRAM
CS 1001 Lecture 13 • PRACTICE EXAM - revisit • SUBPROGRAM • FUNCTION 6 April , 2000
PROGRAM question 1 REAL : : r, circum, area, diameter REAL, PARAMETER : : pi=3. 14159 CHARACTER(1) : : char PRINT *, 'Enter radius' READ *, r PRINT *, 'Enter C, A, or D' READ *, char IF (char. EQ. 'C') THEN circum = 2 * pi * r PRINT *, 'circumference = ', circum ELSE IF (char. EQ. 'A') THEN area = pi * r**2 PRINT *, 'area = ', area ELSE IF (char. EQ. 'D') THEN diameter = 2 * r PRINT *, 'diameter = ', diameter ENDIF END PROGRAM question 1 6 April , 2000
Practice exam # 2 IF (x. LE. 200) THEN IF (x. LT. 100) THEN IF (x. LE. 0) THEN print *, 'A' If (x < 100) ELSE If (x <=200) print *, 'B' ENDIF ELSE If (x >= 100 ) print *, 'C' ENDIF ELSE print *, 'D' x >= 100. AND. x <=200 ENDIF 100 <= X <= 200 6 April , 2000
Practice Exam #3 assign a value of. TRUE. to Even. Number if M is an even number; otherwise assign a value of. FALSE. Even. Number = (M - M/2*2). EQ. 0 or Even. Number = MOD (M, 2). EQ. 0 6 April , 2000
Practice Exam #4 = COND IF ((First. EQ. 'A'). OR. (Last. EQ. 'Z')) THEN IF (Second. EQ. 'B') THEN Second. EQ. 'B' Answer =. TRUE. COND = ELSE. TRUE. Answer =. FALSE. ENDIF ELSE COND = Answer =. TRUE. . FALSE. ENDIF (COND. AND. (Second. EQ. ‘B’)). OR. (COND =. FALSE. ) (COND. AND. (Second. EQ. ‘B’)). OR. . NOT. COND Other answers are possible 6 April , 2000
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 6 April , 2000
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 6 April , 2000
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. Name. . CONTAINS Function(s) go here END PROGRAM Program. Name 6 April , 2000
PROGRAM Temperature_Table IMPLICIT NONE INTEGER : : i. Range, Init, Limit, Step REAL : : Range, Fahr, CTOF Good practice to DO i. Range = Init, Limit, Step declare function Range = REAL (i. Range) type in any Fahr = CTOF(Range) program unit that PRINT *, Range, Fahr calls the function END DO CONTAINS REAL FUNCTION CTOF(Cels) : : END FUNCTION CTOF END PROGRAM Temperature_Table 6 April , 2000
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) 6 April , 2000
Converting With Function DO i. Range = Init, Limit, Step Range = i. Range 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 6 April , 2000
Flow of Control PROGRAM Temperature_Table. . . 1 Fahr = CTOF(Range). . . 7 Range -- actual parameter Cels -- formal parameter 2 Range END PROGRAM Temperature_Table 6 6 April , 2000 3 REAL FUNCTION CTOF (Cels). . . 4 5 CTOF = 1. 8 * Cels + 32. 0 END FUNCTION CTOF
Voltage example PROGRAM Compute. Voltage REAL : : Time, Volts, Voltage Given program statement. . Function Voltage is referenced Time = 2. 5 with an argument of Time = 2. 5 Volts = Voltage (Time) Volts is then set equal to the. . . computed value of 12. 637 CONTAINS FUNCTION Voltage(Time) Expression is evaluated as: (2. 5 + 0. 1) * EXP(SQRT(2. 5)) REAL : : Voltage = 12. 637 REAL, INTENT(IN) : : Time Voltage = (Time + 0. 1)* EXP(SQRT(Time)) END FUNCTION Voltage END PROGRAM Compute. Voltage 6 April , 2000
Function Equivalence • Following are the same Time = 2. 5 Volts = Voltage(Time) Voltage is a FUNCTION 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. 6 April , 2000
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 Callee Used to pass values function_name (formal_argument_list) 6 April , 2000
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) : : argument for inputs TO either a function or subroutine – type, INTENT(OUT) : : argument for 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 6 April , 2000
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. 6 April , 2000
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 April , 2000
Old FORTRAN IV Example In calling program Sum = 0 Sum = addsum (10). . . Sum = Sum + 10 5 Print *, Sum 4 6 1 Name 3 1 In Function addsum (IMAX). . . 2 IMAX = 5 addsum =. . . (say 20) END 6 April , 2000 Sum 2 10 IMAX value in storage 0 10 10, then 5 Name Sum 4 10 value in storage 20 5 Name 5 Sum 10 value in storage 20 5 After 5 Name value in storage 25 5 3 Sum 10 At 6 Print Sum will give 25
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. 6 April , 2000
- Slides: 20