You are on page 1of 50

Building Modular Programs with Procedures

Part 13

Presentation Copyright 2007, Bryan Meyers

Dynamic Program Calls


Calling separate executable program (*Pgm) objects
Objects have no dependency upon each other until run time Never physically connected

System looks for called program when Callp executes Dynamic object resolution process may cause performance problem for callintensive applications
2

Static Binding
Integrated Language Environment (ILE) includes option of connecting modules before runtime
Static binding

Alleviates performance degradation of call

Creating an ILE Program


Two step program creation process
Compile step
CRTRPGMOD creates *MODULE object Each ILE language has its own CRTxxxMOD command

Binding step
CRTPGM creates *PGM object Not language specific

Bind-by-copy
Binding step physically copies code from module to program
4

Creating an ILE Program

Creating an ILE Program


Program can comprise one or more compiled modules
Binding step forms single executable *Pgm

Modules can be written in different languages Binding step designates one module as entry module
First module that will execute

Resulting *Pgm is single entry point program


6

Creating an ILE Program

Procedures and Subprocedures


Procedure is named code segment contained in module object
*MODULE can contain one or more procedures

ILE program consists of main procedure and, optionally, one or more subprocedures

Procedures and Subprocedures


More flexible than subroutines
Can be coded and compiled separately from program that will use them Can accept parameters Support a return value
User defined functions

Can have local variables


Private data

Are recursive
Can call themselves
9

Using Local Procedures


Can include subprocedure in same source member as main procedure
Local procedure Appears after last line of main procedure

Appropriate when
Procedure will not be reused in several programs Need features beyond subroutines Relegate complex code to subprocedure
10

Coding the Main Procedure


Main procedure comprises
H = Control specifications F = File specifications D = Definition specifications
Must include prototype for called subprocedures Definitions in main procedure are global
Can be accessed/changed by any procedure in module

I = Input specifications C = Calculation specifications O = Output specifications


11

Procedure Prototype
Lets compiler compare parameter definitions in calling and called procedures
Checks for inconsistencies that could cause bugs when program runs

Prototype has two parts


Prototype header
May include return value

Parameter description(s)

12

Procedure Prototype
Prototype header
Prototype name (7-21) Definition type (24-25)
PR = Prototype

Procedure may include return value


Length (3339) Data type (40) Decimal positions (4142)
*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8 DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++++++++++++++++++++++++++ D Celsius PR 5 0 D 5 0
13

Procedure Prototype
Parameter description(s) same as program prototype
Comment (7-21)
Optional

Definition type (24-25)


(Blank) = Parameter description

Parameter length (3339) Parameter data type (40) Parameter decimal positions (4142)
*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8 DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++++++++++++++++++++++++++ D Celsius PR 5 0 D 5 0
14

Calling a Procedure
If procedure supports return value, it can be included in expression
Otherwise, use Callp
Metrictemp = Celsius(Englishtemp); If Celsius(Englishtemp) > 0; Status = Thawed; Else; Status = Freezing; Endif;

15

Coding Local Subprocedures


Local subprocedures appear after main procedure Subprocedure begins and ends with Procedure Boundary specifications
Only definitions and calculations can appear between P-specs Definitions in subprocedure are local
Cannot be accessed/changed outside subprocedure
*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8 PName+++++++++++..T...................Functions++++++++++++++++++++++++++++ P Celsius B ... 16 P Celsius E

Coding Local Subprocedures


Local definitions include procedure interface
Specifies return value attributes Accepts parameter(s), assigns value(s) to local variables Must match prototype
Definition type (24-25)
PI = Prototype

Subprocedure interface need not be named


*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8 DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++++++++++++++++++++++++++ D PI 5 0 17 D Fahrenheit 5 0

Coding Local Subprocedures


Local variables are allocated by default in automatic storage
Automatically reinitialized each time procedure is called

To retain values, specify Static keyword


*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8 DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++++++++++++++++++++++++++ D Counter S 5U 0 Static

18

Coding Local Subprocedures


*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8 DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++++++++++++++++++++++++++ // ----------------------------------------------------------- Prototypes D Celsius PR 5 0 D 5 0 // ----------------------------------------------------- Global variables D Metrictemp S 5 0 D Englishtemp S 5 0 Inz(212) D Status S 8 // ------------------------------------------------------- Main procedure /Free Metrictemp = Celsius(Englishtemp); If Celsius(Englishtemp) > 0; Status = Thawed; Else; Status = Freezing; Endif; *Inlr = *On; Return; /End-Free

19 continued

Coding Local Subprocedures

continued

*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8 PName+++++++++++..T...................Functions++++++++++++++++++++++++++++ DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++++++++++++++++++++++++++ // ---------------------------------------------------------------------// // Procedure ... Celsius ... Converts Fahrenheit to Celsius // // ---------------------------------------------------------------------P Celsius B

D D Fahrenheit

PI

5 5

0 0

// ------------------------------------------------------ Local variables D Temperature S 5 0 /Free Eval(H) Temperature = (5/9) * (Fahrenheit - 32); Return Temperature; /End-Free P Celsius E
20

Creating a Single-Module Program


Compile with CRTRPGMOD command Bind with CRTPGM command CRTBNDRPG command combines compile and bind into single step
Must specify DFTACTGRP(*NO) if module contains procedures

21

Creating Nomain Modular Procedures


ILE programs can comprise more than one compile unit
Can be made up of several compiled modules, each representing only portion of program

One module must be entry module


Includes main procedure

Other modules do not have main procedure


Cannot be the entry module Must be called from another procedure
22

Coding the Main Procedure


No coding changes in main procedure Subprocedure prototype is required Actual subprocedure code is not included with main procedure

23

Coding the Main Procedure


*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8 DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++++++++++++++++++++++++++ // ----------------------------------------------------------- Prototypes D Celsius PR 5 0 Complete module D 5 0

THISPGM

// ----------------------------------------------------- Global variables D Metrictemp S 5 0 D Englishtemp S 5 0 Inz(212) D Status S 8 // ------------------------------------------------------- Main procedure /Free Metrictemp = Celsius(Englishtemp); If Celsius(Englishtemp) > 0; Status = Thawed; Else; Status = Freezing; Endif; *Inlr = *On; Return; /End-Free

24

Coding the Nomain Procedures


Minor changes in subprocedure module
H-specs
Includes Nomain keyword to eliminate main procedure

D-specs
Must include prototype

P-specs
Includes Export keyword to make procedure available to other modules in program
Without Export, other modules cannot find procedure

25

Coding the Nomain Procedures


*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8 HKeywords++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ PName+++++++++++..T...................Functions++++++++++++++++++++++++++++ DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++++++++++++++++++++++++++ Complete module H Nomain

CELSIUS

D D

P D D D

// ----------------------------------------------------------- Prototypes Celsius PR 5 0 5 0 // ---------------------------------------------------------------------// Procedure ... Celsius ... Converts Fahrenheit to Celsius // ---------------------------------------------------------------------Celsius B Export PI 5 0 Fahrenheit 5 0 // ------------------------------------------------------ Local variables Temperature S 5 0 /Free Eval(H) Temperature = (5/9) * (Fahrenheit - 32); Return Temperature; /End-Free

P Celsius

26

Creating a Multiple-Module Program


Compile each module with CRTRPGMOD command Bind with CRTPGM command
CRTRPGMOD MODULE(THISPGM) CRTRPGMOD MODULE(CELSIUS) CRTPGM PGM(THISPGM) + MODULE(THISPGM CELSIUS) + ENTMOD(THISPGM)

27

Pass Parameters by Value


Procedures support three parameter passing methods
Pass by reference Pass by read-only reference Pass by value
Pass parameters actual value instead of sharing its address Called procedure must allocate its own memory for parameter

28

Pass Parameters by Value


If called procedure changes parameter, caller does not recognize change
Callers storage is protected

Advantages of pass by value


Parameter values need not be represented in a variable
Expressions, literals, constants allowed

Parameter data types need not exactly match prototype Absolute protection against parameter values being changed by called procedure
29

Pass Parameters by Value


Value keyword specifies pass by value
Applies only to procedure calls (not programs) Must appear in prototype and procedure interface

Can mix parameter passing methods


*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8 DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++++++++++++++++++++++++++ D Celsius PR 5 0 D 5 0 Value ... D PI 5 0 D Fahrenheit 5 0 Value ... /Free Zero = Celsius(32); If Celsius(Englishtemp + 40) > 25; Status = Too hot; Endif; /End-free 30

Using Export and Import


Export and Import allow data sharing between modules
Without passing parameters Parameters are preferred sharing mechanism

Export module allocates storage for data item Import module uses storage allocated by Export module Both modules must be bound to same program
31

Using Export and Import


*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++++++ D Proca PR // Superglobal field D Vara S 9 2 Export Inz(200) /Free Proca(); /End-Free

Primary module

Module PROCA

*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++++++ D Proca PR // Vara gets its value from primary module D Vara S 9 2 Import P Proca B P Proca E

32

Using a Binding Directory


Binding directory alleviates need to list all modules when executing CRTPGM command List of modules that might be reused when binding programs
Object type *BNDDIR
CRTPGM PGM(pgm-name) + MODULE(module-names) + ENTMOD(entry-module) + BNDDIR(binding-directories)
33

Using a Binding Directory


CRTBNDDIR command creates binding directory object WRKBNDDIRE command manages directory entries

34

Updating ILE Programs


If module code is changed and recompiled, it must be rebound to program(s) that use it UPDPGM command abbreviates binding step
List only changed modules Unchanged modules are not affected
UPDPGM PGM(pgm-name) + MODULE(module-names)

35

Updating ILE Programs


Use DSPPGM command to show which modules make up program
DSPPGM PGM(pgm-name) DETAIL(*MODULE)

36

What is a Service Program?


Service program (*Srvpgm) is container for module code that other programs can access
Not standalone executable object

Multiple entry point program


Any module can be an entry point

Single entry point program can invoke code in service program


Code stays in service program Bind-by-reference
37

Creating Service Programs


No unique coding requirements for service program modules
Should be Nomain modules

CRTSRVPGM command binds service program


No entry module
CRTSRVPGM SRVPGM(srvpgm-name) + MODULE(module-names) + BNDDIR(binding-directories)

38

Using Service Programs


Single entry point program or another service program can invoke service program procedures
Cannot call service program Call procedures instead

39

Using Service Programs


Bind service program to caller by reference
BNDSRVPGM parameter

Binding directory can also list service programs


CRTPGM PGM(pgm-name) MODULE(module-names) ENTMOD(entry-module) BNDSRVPGM(srvpgm-names) BNDDIR(binding-directories) + + + +

Bind by copy
Bind by reference

40

Using Service Programs


Service program can also use other service program(s)
CRTSRVPGM SRVPGM(srvpgm-name) + MODULE(module-names) + BNDSRVPGM(srvpgm-names) + BNDDIR(binding-directories)

41

Updating Service Programs


If module code is changed and recompiled, it must be rebound to service program that uses it UPDSRVPGM command abbreviates binding step
List only changed modules Unchanged modules are not affected
UPDSRVPGM PGM(pgm-name) + MODULE(module-names)

42

Updating Service Programs


Use DSPSRVPGM command to show which modules make up program
DSPSRVPGM SRVPGM(pgm-name) DETAIL(*MODULE)

43

Service Program Signature


Service program signature identifies its external interface
Exported procedures (and data items)

Signature is stored in service program and in each program that uses it When caller loads, signature it expects must exist in service program
Otherwise program activation fails

Binder language controls signature


44

Using Binder Language


Three commands that define service program signature
Stored in source member
Not compiled

Source type BND


STRPGMEXP PGMLVL(*CURRENT) EXPORT SYMBOL('DAYOFWEEK') EXPORT SYMBOL('DAYNAME') ENDPGMEXP Source member DATESRVPGM

45

Using Binder Language


Refer to binder language when binding service program
EXPORT(*SRCFILE) points binder to source file to generate signature SRCFILE and SRCMBR name member
CRTSRVPGM SRVPGM(DATESRVPGM) MODULE(DAYOFWEEK DAYNAME) EXPORT(*SRCFILE) SRCFILE(QSRVSRC) SRCMBR(DATESRVPGM) + + + +

46

Using DSPPGM and DSPSRVPGM


Use DSPSRVPGM and DSPPGM commands to analyze service program usage and signature information
Signature must match in service program and in all programs (or service programs) that use it
DSPSRVPGM SRVPGM(srvpgm-name) DETAIL(*SIGNATURE) DSPSRVPGM SRVPGM(srvpgm-name) DETAIL(*SRVPGM) DSPPGM PGM(pgm-name) DETAIL(*SRVPGM)

47

Maintaining Multiple Signatures


Service program can have multiple signatures
Can support multiple interfaces One current signature, many previous signatures

Binder language controls signatures As service program changes, programs that use it may not need to be rebound
As long as signature is still supported
48

Maintaining Multiple Signatures


STRPGMEXP PGMLVL(*CURRENT) EXPORT SYMBOL('DAYOFWEEK') EXPORT SYMBOL('DAYNAME') EXPORT SYMBOL('ENDOFMONTH') ENDPGMEXP STRPGMEXP PGMLVL(*PRV) EXPORT SYMBOL('DAYOFWEEK') EXPORT SYMBOL('DAYNAME') ENDPGMEXP CRTSRVPGM SRVPGM(DATESRVPGM) + MODULE(DAYOFWEEK DAYNAME ENDOFMONTH) + EXPORT(*SRCFILE) + SRCFILE(QSRVSRC) + SRCMBR(DATESRVPGM)
49

Source member DATESRVPGM

Maintaining Multiple Signatures


STRPGMEXP PGMLVL(*CURRENT) EXPORT SYMBOL('DAYOFWEEK') EXPORT SYMBOL('DAYNAME') EXPORT SYMBOL('ENDOFMONTH') EXPORT SYMBOL('ENDOFWEEK') EXPORT SYMBOL('NEXTMONDAY') ENDPGMEXP
STRPGMEXP PGMLVL(*PRV) EXPORT SYMBOL('DAYOFWEEK') EXPORT SYMBOL('DAYNAME') EXPORT SYMBOL('ENDOFMONTH') ENDPGMEXP STRPGMEXP PGMLVL(*PRV) EXPORT SYMBOL('DAYOFWEEK') EXPORT SYMBOL('DAYNAME') ENDPGMEXP

Source member DATESRVPGM

50

You might also like