Download presentation
Presentation is loading. Please wait.
1
SUB-PROGRAMS MET 50
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. 11/10/2011 MET 50, FALL 2011, CHAPTER 6 PART 1 2
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. 11/10/2011 MET 50, FALL 2011, CHAPTER 6 PART 1 3
4
Using sub-programs Modular programming : http://en.wikipedia.org/wiki/Modular_programming http://en.wikipedia.org/wiki/Modular_programming GFDL site: http://www.gfdl.noaa.gov/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) 11/10/2011 MET 50, FALL 2011, CHAPTER 6 PART 1 4
5
Using sub-programs Example: To make a weather forecast…. We need to pay attention to: 1. Solar radiation heating – compute in one code 2. Longwave radiation cooling – compute in one code 3. Cloud cover – compute in one code 4. Evaporation from the ocean – compute in one code 5. etc etc. 11/10/2011 MET 50, FALL 2011, CHAPTER 6 PART 1 5
6
Using sub-programs It makes sense to write a separate chunk of code for each task! Makes initial development easier Allows upgrades easily 11/10/2011 MET 50, FALL 2011, CHAPTER 6 PART 1 6
7
Using sub-programs Types of sub-program: (A) Functions – simpler (B) Subroutines – more sophisticated 11/10/2011 MET 50, FALL 2011, CHAPTER 6 PART 1 7
8
Using sub-programs In BOTH cases, we: 1. Send some information from the main program to the sub-program 2. The sub-program does some calculation(s) 3. The sub-program returns an value to the main program. 11/10/2011 MET 50, FALL 2011, CHAPTER 6 PART 1 8
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. 11/10/2011 MET 50, FALL 2011, CHAPTER 6 PART 1 9
10
Using sub-programs We don’t get to see the code behind these. Sometimes, we build our own functions! The code looks like: 11/10/2011 MET 50, FALL 2011, CHAPTER 6 PART 1 10
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 11/10/2011 MET 50, FALL 2011, CHAPTER 6 PART 1 11
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 11/10/2011 MET 50, FALL 2011, CHAPTER 6 PART 1 12
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 11/10/2011 MET 50, FALL 2011, CHAPTER 6 PART 1 13
14
Using sub-programs So: 1. YOU get to decide which pieces of code to assign to a function. 2. YOU get to write the code for that function. 3. YOU get to put code in the MAIN PROGRAM which gets information from the function. And: 4. YOU get to decide where to put the “function code”. 5. ??? 11/10/2011 MET 50, FALL 2011, CHAPTER 6 PART 1 14
15
Using sub-programs There are two places we can put the “function code”: 1. At the end of the main code/after the main code. 2. At the end of the main code/inside the main code. Going back to our TEMPCON and f_to_c example… 11/10/2011 MET 50, FALL 2011, CHAPTER 6 PART 1 15
16
Using sub-programs 1. 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 11/10/2011 MET 50, FALL 2011, CHAPTER 6 PART 1 16
17
Using sub-programs 2. 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 11/10/2011 MET 50, FALL 2011, CHAPTER 6 PART 1 17
18
Using sub-programs 3. Main code could look like: PROGRAM MAIN IMPLICIT NONE REAL :: bla bla bla whatever CONTAINS ← new KEYWORD FUNCTION sub1 (X1) bla bla bla RETURN END FUNCTION sub1 FUNCTION sub2 (X2) bla bla bla RETURN END FUNCTION sub2 etc. END PROGRAM MAIN 11/10/2011 MET 50, FALL 2011, CHAPTER 6 PART 1 18
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. 11/10/2011 MET 50, FALL 2011, CHAPTER 6 PART 1 19
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.