190 likes | 292 Views
MET 50. Sub-programs. Subroutines. Programmers prefer to break the overall task into smaller pieces. Then we write individual “sub-programs” to do each task!. Subroutines. Functions : Last lecture. Subroutines. (B) Subroutines :
E N D
MET 50 Sub-programs
Subroutines Programmers prefer to break the overall task into smaller pieces. Then we write individual “sub-programs” to do each task! MET 50, FALL 2010, CHAPTER 7
Subroutines Functions: Last lecture MET 50, FALL 2010, CHAPTER 7
Subroutines (B) Subroutines: When using a function, we only get ONE piece of information back. A subroutine is more powerful – we can get MULTIPLE pieces of information back. MET 50, FALL 2010, CHAPTER 7
Subroutines Subroutines are often used to do manipulations involving arrays. Examples: Given a matrix A, take the inverse of the matrix, A-1. Compute the kinetic energy of air, assuming we know the wind. MET 50, FALL 2010, CHAPTER 7
Subroutines What types of things are done in subroutines? • Operations that we do frequently. We can write one subroutine (ONE set of code), and use it a million times. • Alternatively, we can write the same code a million times. • Operations that can be easily split away from the main code. MET 50, FALL 2010, CHAPTER 7
Subroutines A SUBROUTINE has no value associated with its name. All outputs are defined in terms of arguments; there may be any number of outputs. MET 50, FALL 2010, CHAPTER 7
Subroutines • A SUBROUTINE is not called into action simply by writing its name. • Instead, we write a CALL statement to bring it into operation. • The CALL statement does two things: • specifies the arguments • results in storing all the output values. MET 50, FALL 2010, CHAPTER 7
Subroutines Example 1 • Write a subroutine to compute and return sin(x), cos(x), and tan(x), given x. • Let’s call it: TWIG • Input: • x • Output: • s=sin(x) • c=cos(x) • t=tan(x)=sin(x)/cos(x) MET 50, FALL 2010, CHAPTER 7
Subroutines Our subroutine will be called from inside the main program via the CALL statement, as in: CALL TWIG(x,s,c,t) inputoutput We can write arguments in any order we like, e.g., instead CALL TWIG(s,c,t,x) MET 50, FALL 2010, CHAPTER 7
Subroutines And then the subroutine itself (the code) begins with the same name and argument list – in the same order, as in: SUBROUTINE TWIG(x,s,c,t) MET 50, FALL 2010, CHAPTER 7
Subroutines Note: we do NOT need to use the same letters in both lines of code. So it would be OK to have this… CALL TWIG(x,s,c,t) …later in code… SUBROUTINE TWIG(xin,ssin,ccos,ttan) …code… MET 50, FALL 2010, CHAPTER 7
Subroutines This equates: x xin s ssin c ccos t ttan Values are shared between names as above. MET 50, FALL 2010, CHAPTER 7
Subroutines The subroutine code itself – after the 1st line – is a stand-alone piece of code. Example from above: SUBROUTINE TWIG(XIN, SSIN, CCOS, TTAN) ! IMPLICIT NONE REAL :: XIN, SSIN, CCOS, TTAN ! SSIN=SIN(XIN) CCOS=COS(XIN) TTAN=SSIN/CCOS ! END SUBROUTINE TWIG MET 50, FALL 2010, CHAPTER 7
Subroutines And in the main code: …code… …specify value of “x” – read in or set …before here, the value of “x” is set, but the values of “s”, “c”, and “t” are unknown CALL TWIG(x,s,c,t) …after the call to the subroutine, the values of “s”, “c”, and “t” are returned from the subroutine and are thus known! …more code (using s, c, and t) MET 50, FALL 2010, CHAPTER 7
Subroutines Example 2 • Write a subroutine to compute and return the sum of two arrays, A(M,N) and B(M,N). • Let’s call it: ARRSUM • Input: • A, B, M, N – you will need to provide array dimensions • Output: • C = A + B MET 50, FALL 2010, CHAPTER 7
Subroutines To add two arrays, each 20 x 40 in size, we might have: Inside the main program… REAL, DIMENSION(20,40) :: A, B, C INTEGER :: M, N …input/specify values of M, N, A, and B …more code… …at this point, the value of C is unknown… CALL ARRSUM(A, B, M, N, C) …this returns the value of C = A + B …more code… MET 50, FALL 2010, CHAPTER 7
Subroutines SUBROUTINE ARRSUM(AIN, BIN, M, N, COUT) ! INTEGER :: M, N REAL, DIMENSION (M,N) :: AIN, BIN, COUT ! DO I = 1, M DO J = 1, N COUT(I, J) = AIN(I, J) + BIN(I, J) END DO ENDDO END SUBROUTINE ARRSUM MET 50, FALL 2010, CHAPTER 7
Subroutines How do we insert subroutine code into main code ? Same choices as with functions: At end Internally using CONTAINS statement MET 50, FALL 2010, CHAPTER 7