1 / 29

Subroutines

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

pelaez
Download Presentation

Subroutines

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

  2. Outline of subprograms • Functions • built-in • programmer defined • internal • external • Subroutines • built-in (like RANDOM_NUMBER) • programmer defined

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

  4. 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 PrintInstructions()

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

  6. Program structure • Subroutines are declared in the CONTAINS section (just like functions) • CALL statements are always used to invoke a subroutine.

  7. Example: subroutine with no arguments CALL Instruct() 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 Called from main program

  8. Control transfers to subroutine Main program CALL Instruct() Subroutine instruct SUBROUTINE Instruct() END SUBROUTINE Instruct

  9. Transfer back to main Main program CALL Instruct() Subroutine instruct SUBROUTINE Instruct() END SUBROUTINE Instruct

  10. Example:subroutine with one argument CALL Table(n) 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 Called from main program

  11. Control transfers to subroutine Main program CALL Table(n) Subroutine instruct SUBROUTINE Table(n) INTEGER, INTENT(IN) :: n END SUBROUTINE Table

  12. Transfer back to main Main program CALL Table(n) Subroutine Table SUBROUTINE Table(n) END SUBROUTINE Table

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

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

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

  16. Structure diagram Main Program Read data Process data Print results Find sum Find average

  17. What are these modules? • Eventually, we will be able to take a structure diagram like the one 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!

  18. Structure diagram Main Program Read data Process data Print results Find sum Find average Good function candidates

  19. Structure diagram Good subroutine candidates Main Program Read data Process data Print results Find sum Find average

  20. Structure Chart Boxes • Example: getting from UMD to MetroDome • Algorithm: Subtask 1: Get in car at parking lot … Subtask 2: Drive to Minneapolis Subtask 3: Find MetroDome Subtask 4: Get out of car

  21. 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 I35 2.2 Drive south to I35W 2.3 Drive south to Minneapolis • Each division of a task is a “stepwise refinement”

  22. Stepwise Refinement • Do stepwise refinement until all tasks easy • Example: 2.1 - Drive east to I35 2.1.1 Exit parking lot to East 2.1.2 Turn right on Woodland 2.1.3 Turn left on 21st 2.1.4 Enter I35 • This process is know as Top-Down Design

  23. Multi-layer Structure Chart

  24. Another Example Problem: Balance checkbook Top-level tasks 1. Get information 2. Perform computations 3. Print results

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

  26. The subroutine shortcut PROGRAM Checkbook REAL :: balance, amount CHAR :: type CALL GetInfo(balance, type, amount) CALL Process(balance, type, amount) CALL Results(balance, type, amount) END PROGRAM Checkbook 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

  27. The subroutine shortcut PROGRAM Checkbook REAL :: balance, amount, newbalance INTEGER :: type CALL GetInfo(balance, type, amount) CALL Process(balance, type, amount, & newbalance) CALL Results(balance, type, amount, & newbalance) CONTAINS SUBROUTINE GetInfo(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 GetInfo 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

  28. The subroutine shortcut PROGRAM Checkbook REAL :: balance, amount, newbalance INTEGER :: type CALL GetInfo(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 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

  29. The subroutine shortcut PROGRAM Checkbook REAL :: balance, amount, newbalance CHAR :: type CALL GetInfo(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 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

More Related