Download presentation
Presentation is loading. Please wait.
1
SUB-PROGRAMS MET 50
2
Subroutines Programmers prefer to break the overall task into smaller pieces. Then we write individual “sub-programs” to do each task! 11/18/2010 MET 50, FALL 2010, CHAPTER 7 2
3
Subroutines (A) Functions: Last lecture 11/18/2010 MET 50, FALL 2010, CHAPTER 7 3
4
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. 11/18/2010 MET 50, FALL 2010, CHAPTER 7 4
5
Subroutines Subroutines are often used to do manipulations involving arrays. Examples: 1. Given a matrix A, take the inverse of the matrix, A -1. 2. Compute the kinetic energy of air, assuming we know the wind. 11/18/2010 MET 50, FALL 2010, CHAPTER 7 5
6
Subroutines What types of things are done in subroutines? 1. 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. 2. Operations that can be easily split away from the main code. 11/18/2010 MET 50, FALL 2010, CHAPTER 7 6
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. 11/18/2010 MET 50, FALL 2010, CHAPTER 7 7
8
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. 11/18/2010 MET 50, FALL 2010, CHAPTER 7 8
9
Subroutines Example 1 1. Write a subroutine to compute and return sin(x), cos(x), and tan(x), given x. 2. Let’s call it: TWIG 3. Input: 1. x 4. Output: 1. s=sin(x) 2. c=cos(x) 3. t=tan(x)=sin(x)/cos(x) 11/18/2010 MET 50, FALL 2010, CHAPTER 7 9
10
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) 11/18/2010 MET 50, FALL 2010, CHAPTER 7 10
11
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) 11/18/2010 MET 50, FALL 2010, CHAPTER 7 11
12
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… 11/18/2010 MET 50, FALL 2010, CHAPTER 7 12
13
Subroutines This equates: x xin s ssin c ccos t ttan Values are shared between names as above. 11/18/2010 MET 50, FALL 2010, CHAPTER 7 13
14
Subroutines The subroutine code itself – after the 1 st 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 11/18/2010 MET 50, FALL 2010, CHAPTER 7 14
15
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) 11/18/2010 MET 50, FALL 2010, CHAPTER 7 15
16
Subroutines Example 2 1. Write a subroutine to compute and return the sum of two arrays, A(M,N) and B(M,N). 2. Let’s call it: ARRSUM 3. Input: 1. A, B, M, N – you will need to provide array dimensions 4. Output: 1. C = A + B 11/18/2010 MET 50, FALL 2010, CHAPTER 7 16
17
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… 11/18/2010 MET 50, FALL 2010, CHAPTER 7 17
18
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 END SUBROUTINE ARRSUM 11/18/2010 MET 50, FALL 2010, CHAPTER 7 18
19
Subroutines How do we insert subroutine code into main code ? Same choices as with functions: a) At end b) Internally using CONTAINS statement 11/18/2010 MET 50, FALL 2010, CHAPTER 7 19
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.