Subroutines Outline of subprograms Functions builtin programmer defined
Subroutines
Outline of subprograms • Functions – built-in – programmer defined • internal • external • Subroutines – built-in (like RANDOM_NUMBER) – programmer defined
Functions • • are designed to return a single value return the value associated with the name are typed are referenced by using their name in an expression, such as: – PRINT*, SQRT(n) – To simply have a line like this would make no sense: – SQRT(n)
Subroutines • May return no values, one value, or many values • Return the data through the arguments • Are not typed • Are referenced through a separate CALL statement: – CALL Print. Instructions()
Built-in Subroutines • A comprehensive list of built-in subroutines and functions is given in Appendix D. • It includes explanations for how each one works. • Examples of interesting ones: DATE_AND_TIME(date, time, zone, values) SYSTEM_CLOCK(count, count_rate, count_max) RANDOM_NUMBER(harvest) RANDOM_SEED(size, put, get)
Program structure • Subroutines are declared in the CONTAINS section (just like functions) • CALL statements are always used to invoke a subroutine.
Example: subroutine with no arguments CALL Instruct() Called from main program SUBROUTINE Instruct() PRINT*, “Please enter today’’s date in the form” PRINT*, “month, day, year” PRINT*, “like this: 4, 24, 1999” PRINT*, “Do not put slashes between the different” PRINT*, “parts of the date. ” END SUBROUTINE Instruct
Control transfers to subroutine Main program CALL Instruct() Subroutine instruct SUBROUTINE Instruct() END SUBROUTINE Instruct
Transfer back to main Main program CALL Instruct() Subroutine instruct SUBROUTINE Instruct() END SUBROUTINE Instruct
Example: subroutine with one argument CALL Table(n) Called from main program SUBROUTINE TABLE(n) INTEGER, INTENT(IN) : : n INTEGER i PRINT*, “This subroutine prints a table of “, n, & “ASCII characters” PRINT*, “i ASCII” DO i=0, n-1 PRINT*, i, CHAR(i) END DO END SUBROUTINE Table
Control transfers to subroutine Main program CALL Table(n) Subroutine instruct SUBROUTINE Table(n) INTEGER, INTENT(IN) : : n END SUBROUTINE Table
Transfer back to main Main program CALL Table(n) Subroutine Table SUBROUTINE Table(n) END SUBROUTINE Table
Example: subroutine with multiple arguments CALL Factorial(n, nfact) SUBROUTINE Factorial(n, nfact) INTEGER, INTENT(IN) : : n INTEGER, INTENT(OUT) : : nfact INTEGER I IF (n < 0) THEN PRINT*, “n must be negative” ELSE IF (n = 0) THEN nfact = 1 ELSE nfact = 1 DO i=1, n nfact = nfact * i END DO END SUBROUTINE Factorial Called from main program
Control transfers to subroutine Main program CALL Factorial(n, nfact) Subroutine instruct SUBROUTINE Factorial(n, nfact) INTEGER, INTENT(IN) : : n INTEGER, INTENT(OUT) : : nfact END SUBROUTINE Factorial
Transfer back to main Main program CALL Factorial(n, nfact) Subroutine Factorial SUBROUTINE Factorial(n, nfact) INTEGER, INTENT(IN) : : n INTEGER, INTENT(OUT) : : nfact END SUBROUTINE Factorial
Structure diagram Main Program Read data Process data Find sum Print results Find average
What are these modules? • Eventually, we will be able to take a structure diagram like the on the last slide and turn it into a modular program. • Now, however, we do not have the tools to make a module out of something like ‘Read data’ • We do have the tools to modularize ‘Find sum’ or ‘Find average’ - just make them internal functions!
Structure diagram Main Program Read data Process data Find sum Good function candidates Print results Find average
Structure diagram Good subroutine candidates Main Program Read data Process data Find sum Print results Find average
Structure Chart Boxes • Example: getting from UMD to Metro. Dome • Algorithm: Subtask 1: Get in car at parking lot … Subtask 2: Drive to Minneapolis Subtask 3: Find Metro. Dome Subtask 4: Get out of car
Developing an Algorithm (cont) • Strategy (cont): repetitively divide tasks until each task is easily solved • Example: Subtask 2 - Drive to Minneapolis 2. 1 Drive east to I 35 2. 2 Drive south to I 35 W 2. 3 Drive south to Minneapolis • Each division of a task is a “stepwise refinement”
Stepwise Refinement • Do stepwise refinement until all tasks easy • Example: 2. 1 - Drive east to I 35 2. 1. 1 Exit parking lot to East 2. 1. 2 Turn right on Woodland 2. 1. 3 Turn left on 21 st 2. 1. 4 Enter I 35 • This process is know as Top-Down Design
Multi-layer Structure Chart
Another Example Problem: Balance checkbook Top-level tasks 1. Get information 2. Perform computations 3. Print results
Pseudo-code Example 1. Get information 1. 1 Get starting balance 1. 2 Get transaction type 1. 3 Get transaction amount 2. Perform computations 2. 1 IF deposit THEN add to balance ELSE subtract from balance 3. Print results 3. 1 Print starting balance 3. 2. Print transaction 3. 2. 1 Print transaction type 3. 2. 2 Print transaction amount 3. 3 Print ending balance
The subroutine shortcut 1. Get information 1. 1 Get starting balance 1. 2 Get transaction type 1. 3 Get transaction amount PROGRAM Checkbook REAL : : balance, amount CHAR : : type CALL Get. Info(balance, type, amount) CALL Process(balance, type, amount) CALL Results(balance, type, amount) 2. Perform computations 2. 1 IF deposit THEN add to balance 2. 2 ELSE subtract from balance 3. Print results 3. 1 Print starting balance 3. 2. Print transaction 3. 2. 1 Print transaction type 3. 2. 2 Print transaction amount 3. 3 Print ending balance END PROGRAM Checkbook
The subroutine shortcut 1. Get information 1. 1 Get starting balance 1. 2 Get transaction type 1. 3 Get transaction amount 2. Perform computations 2. 1 IF deposit THEN add to balance 2. 2 ELSE subtract from balance 3. Print results 3. 1 Print starting balance 3. 2. Print transaction 3. 2. 1 Print transaction type 3. 2. 2 Print transaction amount 3. 3 Print ending balance PROGRAM Checkbook REAL : : balance, amount, newbalance INTEGER : : type CALL Get. Info(balance, type, amount) CALL Process(balance, type, amount, & newbalance) CALL Results(balance, type, amount, & newbalance) CONTAINS SUBROUTINE Get. Info(balance, type, amount) REAL, INTENT(OUT) : : balance, amount INTEGER, INTENT(OUT) : : type PRINT*, “Please enter the account balance” READ*, balance PRINT*, “What type of transaction? “ PRINT*, “ 1. Deposit” PRINT*, “ 2. Withdrawal” READ*, type PRINT*, “Enter transaction amount” READ*, amount END SUBROUTINE Get. Info
The subroutine shortcut 1. Get information 1. 1 Get starting balance 1. 2 Get transaction type 1. 3 Get transaction amount 2. Perform computations 2. 1 IF deposit THEN add to balance 2. 2 ELSE subtract from balance 3. Print results 3. 1 Print starting balance 3. 2. Print transaction 3. 2. 1 Print transaction type 3. 2. 2 Print transaction amount 3. 3 Print ending balance PROGRAM Checkbook REAL : : balance, amount, newbalance INTEGER : : type CALL Get. Info(balance, type, amount) CALL Process(balance, type, amount, & newbalance) CALL Results(balance, type, amount, & newbalance) CONTAINS …(continued) SUBROUTINE Process(balance, type, & amount, newbalance) REAL, INTENT(INOUT) : : newbalance INTEGER, INTENT(IN) : : type REAL, INTENT(IN) : : balance, amount IF (type == 1) THEN newbalance = balance + amount ELSE newbalance = balance - amount ENDIF END SUBROUTINE Process
The subroutine shortcut 1. Get information 1. 1 Get starting balance 1. 2 Get transaction type 1. 3 Get transaction amount 2. Perform computations 2. 1 IF deposit THEN add to balance 2. 2 ELSE subtract from balance 3. Print results 3. 1 Print starting balance 3. 2. Print transaction 3. 2. 1 Print transaction type 3. 2. 2 Print transaction amount 3. 3 Print ending balance PROGRAM Checkbook REAL : : balance, amount, newbalance CHAR : : type CALL Get. Info(balance, type, amount) CALL Process(balance, type, amount, & newbalance) CALL Results(balance, type, amount, & newbalance) CONTAINS …(continued) SUBROUTINE Results(balance, type, & amount, newbalance) INTEGER, INTENT(IN) : : type REAL, INTENT(IN) : : balance, amount, & newbalance PRINT*, “Starting balance: $”, balance IF (type == 1) THEN PRINT*, “Deposit” ELSE PRINT*, “Withdrawal” ENDIF PRINT*, “Amount $”, amount PRINT*, “New balance: $”, newbalance END SUBROUTINE Results
- Slides: 29