1 / 19

MET 50

MET 50. Sub-programs. Using sub-programs. The codes we/you have written so far are teeny (10-40 lines). In science & engineering, many codes are huge . Thousands of lines of code. Using sub-programs. Programmers 99.999% prefer to break the overall task into smaller pieces.

elsa
Download Presentation

MET 50

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. MET 50 Sub-programs

  2. Using sub-programs The codes we/you have written so far are teeny (10-40 lines). In science & engineering, many codes are huge. Thousands of lines of code. MET 50, FALL 2011, CHAPTER 6 PART 1

  3. Using sub-programs Programmers 99.999% prefer to break the overall task into smaller pieces. Then we write individual “sub-programs” to do each task! Each piece is a module. MET 50, FALL 2011, CHAPTER 6 PART 1

  4. Using sub-programs • Modular programming : http://en.wikipedia.org/wiki/Modular_programming • GFDL site: http://www.gfdl.noaa.gov/ • → model development • → atmospheric • → model development • → The AM2 model • → Fortran module documentation • → huge list of modules (e.g., ice-model.html) MET 50, FALL 2011, CHAPTER 6 PART 1

  5. Using sub-programs Example: To make a weather forecast…. We need to pay attention to: Solar radiation heating – compute in one code Longwave radiation cooling – compute in one code Cloud cover – compute in one code Evaporation from the ocean – compute in one code etc etc. MET 50, FALL 2011, CHAPTER 6 PART 1

  6. Using sub-programs It makes sense to write a separate chunk of code for each task! Makes initial development easier Allows upgrades easily MET 50, FALL 2011, CHAPTER 6 PART 1

  7. Using sub-programs Types of sub-program: Functions – simpler Subroutines – more sophisticated MET 50, FALL 2011, CHAPTER 6 PART 1

  8. Using sub-programs In BOTH cases, we: Send some information from the main program to the sub-program The sub-program does some calculation(s) The sub-program returns an value to the main program. MET 50, FALL 2011, CHAPTER 6 PART 1

  9. Using sub-programs (A) Functions: We have already met intrinsic functions. Built in to the Fortran compiler. Things like “sin(x)” When we write: Y = SIN(X), our code is calling an (intrinsic) function. MET 50, FALL 2011, CHAPTER 6 PART 1

  10. Using sub-programs We don’t get to see the code behind these. Sometimes, we build our own functions! The code looks like: MET 50, FALL 2011, CHAPTER 6 PART 1

  11. Using sub-programs FUNCTION name (arguments) Usual Fortran statements Execution statements RETURN END FUNCTION name name has to obey usual rules “arguments” are values sent over from the main program “RETURN” statement sends results back to the main program MET 50, FALL 2011, CHAPTER 6 PART 1

  12. Using sub-programs Example: FUNCTION f_to_c(Temperature) ! or… REAL FUNCTION f_to_c(Temperature) ! ! Input: Temperature = temp in deg F ! Output: f_to_c= temp in deg C ! IMPLICIT NONE REAL :: f_to_c, Temperature ! f_to_c= (Temperature - 32.0)*9./5. ! RETURN END FUNCTION f_to_c MET 50, FALL 2011, CHAPTER 6 PART 1

  13. Using sub-programs How is this module “summoned” from the main code??? PROGRAM TEMPCON ! This code reads in a temperature (F) and converts to Celcius IMPLICIT NONE REAL :: TEMPIN, TEMP ! PRINT*,’ENTER A TEMPERATURE IN DEGREES F:’ READ*,TEMPIN ! example: read in TEMPIN = 82. ! Convert using function f_to_c TEMP =f_to_c(TEMPIN)! Sends the value 82. to the function to be converted ! PRINT*,TEMPIN,TEMP ! Prints 82. and 28. (try it!) ! END PROGRAM TEMPCON MET 50, FALL 2011, CHAPTER 6 PART 1

  14. Using sub-programs So: YOU get to decide which pieces of code to assign to a function. YOU get to write the code for that function. YOU get to put code in the MAIN PROGRAM which gets information from the function. And: YOU get to decide where to put the “function code”. ??? MET 50, FALL 2011, CHAPTER 6 PART 1

  15. Using sub-programs There are two places we can put the “function code”: At the end of the main code/after the main code. At the end of the main code/inside the main code. Going back to our TEMPCON and f_to_cexample… MET 50, FALL 2011, CHAPTER 6 PART 1

  16. Using sub-programs At the end of the main code/after the main code. PROGRAM TEMPCON IMPLICIT NONE REAL :: TEMPIN, TEMP PRINT*,’ENTER A TEMPERATURE IN DEGREES F:’ READ*,TEMPIN TEMP =f_to_c(TEMPIN) PRINT*,TEMPIN,TEMP END PROGRAM TEMPCON FUNCTION f_to_c (Temperature) ! Input: Temperature = temp in deg F ! Output: f_to_c = temp in deg C IMPLICIT NONE REAL :: f_to_c, Temperature f_to_c = (Temperature - 32.0)*9./5. RETURN END FUNCTION f_to_c MET 50, FALL 2011, CHAPTER 6 PART 1

  17. Using sub-programs At the end of the main code/inside the main code. PROGRAM TEMPCON IMPLICIT NONE REAL :: TEMPIN, TEMP PRINT*,’ENTER A TEMPERATURE IN DEGREES F:’ READ*,TEMPIN TEMP =f_to_c(TEMPIN) PRINT*,TEMPIN,TEMP CONTAINS ← new KEYWORD FUNCTION f_to_c (Temperature) ! Input: Temperature = temp in deg F ! Output: f_to_c = temp in deg C IMPLICIT NONE REAL :: f_to_c, Temperature f_to_c = (Temperature - 32.0)*9./5. RETURN END FUNCTION f_to_c END PROGRAM TEMPCON MET 50, FALL 2011, CHAPTER 6 PART 1

  18. Using sub-programs Main code could look like: PROGRAM MAIN IMPLICIT NONE REAL :: blablabla whatever CONTAINS ← new KEYWORD FUNCTION sub1 (X1) blablabla RETURN END FUNCTION sub1 FUNCTION sub2 (X2) blablabla RETURN END FUNCTION sub2 etc. END PROGRAM MAIN MET 50, FALL 2011, CHAPTER 6 PART 1

  19. Using sub-programs (B) Subroutines: With a function, we can only get ONE piece of information back. With a subroutine, we can get MULTIPLE pieces of information back  much more powerful. MET 50, FALL 2011, CHAPTER 6 PART 1

More Related