RPG: Combining Multiple Modules into a Service Program

Learn how to create a service program with multiple modules in RPG and use the procedures from these modules in your RPG applications, promoting code reusability and maintainability.

Combining Multiple Modules into a Service Program in RPG

In RPG, you can create service programs with multiple modules, which enables you to build modular, reusable, and maintainable applications. In this tutorial, we’ll walk you through creating a service program that contains two modules, each with a specific procedure, and demonstrate how to use the procedures in an RPG program.

Step 1: Create Two Modules and a Source Member

Create two modules, named `ModuleA` and `ModuleB`, with the following source code:

ModuleA:

**free
ctl-opt nomain;

dcl-proc ProcedureA;
  dcl-pi *n int(10);
  end-pi;

  return 42;
end-proc;

ModuleB:

**free
ctl-opt nomain;

dcl-proc ProcedureB;
  dcl-pi *n int(10);
  end-pi;

  return 84;
end-proc;

Create a source member called `EXPORTS` in your source file with the following content:

EXPORT
  SYMBOL('ProcedureA')
  SYMBOL('ProcedureB')

Step 2: Compile Both Modules

Compile both modules using the Create RPG Module (CRTBNDRPG) command:

CRTBNDRPG PGM(MYLIB/MODULEA) SRCFILE(MYLIB/QRPGLESRC) SRCMBR(MODULEA) DBGVIEW(*SOURCE)

CRTBNDRPG PGM(MYLIB/MODULEB) SRCFILE(MYLIB/QRPGLESRC) SRCMBR(MODULEB) DBGVIEW(*SOURCE)

Step 3: Create a Service Program with Exported Procedures

Create a service program called `MySrvPgm` that binds both modules together and uses the `EXPORTS` source member to determine which procedures to export:

CRTSRVPGM SRVPGM(MYLIB/MYSRVPGM) MODULE(MYLIB/MODULEA MYLIB/MODULEB) EXPORT(*SRCFILE MYLIB/QRPGLESRC EXPORTS) DETAIL(*BASIC) STGMDL(*INHERIT)

Step 4: Define External Procedures in Your RPG Program

In your RPG program, define the external procedures from both modules:

**free
ctl-opt main(MainProcedure);

dcl-pr ProcedureA int(10) extproc('MODULEA/ProcedureA');
end-pr;

dcl-pr ProcedureB int(10) extproc('MODULEB/ProcedureB');
end-pr;

dcl-proc MainProcedure;
  dcl-s resultA int(10);
  dcl-s resultB int(10);

  resultA = ProcedureA();
  resultB = ProcedureB();

  dsply ('ResultA: ' + %char(resultA));
  dsply ('ResultB: ' + %char(resultB));
end-proc;

Step 5: Compile the RPG Program

Compile the RPG program:

CRTBNDRPG PGM(MYLIB/MYPGM) SRCFILE(MYLIB/QRPGLESRC) SRCMBR(MYPGM) DBGVIEW(*SOURCE)

Step 6: Bind the Service Program to Your Program

Bind the service program to your program:

CRTRPGPGM PGM(MYLIB/MYPGM) MODULE(MYLIB/MYPGM) BNDSRVPGM((MYLIB/MYSRVPGM)) DETAIL(*BASIC)

Step 7: Run the Program

Run the program:

CALL PGM(MYLIB/MYPGM)

After running the program, you’ll see the following output:

ResultA: 42
ResultB: 84

Conclusion

In this tutorial, we demonstrated how to create a service program with multiple modules in RPG and use their procedures in an RPG application. By combining related procedures from different modules into a single service program, you can promote code reusability and maintainability, streamline your development process, and build more robust and scalable applications on the IBMi platform.

Leave a comment