|
Booth wrote:
>Hans, I'd love to use the example in this post but I
>don't understand it. Learning them out of book is
>not easy. This is a good example. Simple
>process and clearly explained yet I have no idea
>where to even start.
I bet you have more of an idea than you think! When you define a subroutine
to do some work, you do the same analysis as you do when you write a
program: What input do I need to get? What output do I need to generate?
Then you put a BEGSR/ENDSR pair and write some code. That code uses
"global" variables - variables that are known and used outside the
subroutine. That makes it easy for the mainline to stomp on variables used
in your subroutine and vice-versa.
Procedures are "grown up" subroutines - they have their own private
variables that the mainline doesn't touch. They are also "baby programs" -
they can get input and return output through parameters.
Don't let the "newness" bother you too much. You'll get over it quickly
enough...really!
This example is a quick comparison between a subroutine and a subprocedure.
Its usefulness is somewhat limited, but it IS fairly short:
* dftactgrp(*no) actgrp(qile) dbgview(*source)
* Example of converting a subroutine into a procedure
* Procedure prototype - tells the compiler what to expect
* when it sees the procedure in use
DExtractCityP pr
D AddrInp 50 const
D CityOut 25
* Work fields (global because they are defined outside of any
* subprocedures )
D Address s 50 inz('Albany, NY 12211')
D CityP s 25 inz
* Use the subroutine
C Exsr ExtractCityS
* Use the subprocedure
C CallP ExtractCityP(Address: CityP)
C Eval *InLR = *On
* Extract city from address line
C ExtractCityS Begsr
C MoveL(p) Address AddrWork 50
C* Look for the delimiter between city and state
C ',' scan AddrWork Delim 2 0
C* Extract from first column to the column before the delimiter
C Delim IfGT 0
C Sub 1 Delim
C Delim subst(p) AddrWork:1 City
C* Didn't find delimiter - default to use leftmost part of address
C Else
C MoveL(p) AddrWork City 25
C EndIf
C Endsr
* Procedure interface - tells the compiler what to perform
* when the procedure is called. Basically
* the same as the BEGSR/ENDSR pair.
PExtractCityP b
DExtractCityP pi
D AddrInp 50 const
D CityOut 25
* Work fields (local because they are defined inside this
* subprocedure. Note that this Delim is an
* integer while the one in the subroutine
* is packed. This Delim is only known within
* this procedure. )
D Delim s 10i 0
* Extract city from address line
C* Look for the delimiter between city and state
C eval Delim = 0
C eval Delim = %scan(',': AddrInp)
C* Extract from first column to the column before the delimiter
C If Delim>0
C eval CityOut = %subst(AddrInp: 1: Delim-1)
C* Didn't find delimiter - default to use leftmost part of address
C Else
C Eval CityOut = *Blanks
C EndIf
PExtractCityP e
Buck
+---
| This is the RPG/400 Mailing List!
| To submit a new message, send your mail to RPG400-L@midrange.com.
| To subscribe to this list send email to RPG400-L-SUB@midrange.com.
| To unsubscribe from this list send email to RPG400-L-UNSUB@midrange.com.
| Questions should be directed to the list owner/operator: david@midrange.com
+---
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2025 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.