RPG Programming Language – How to Learn Easily

Real usage of RPG is in AS/400. In RPG you can embed SQL statements. Handling strings possible with VARCHAR data type.

What is RPG

RPG is another programming language. Always you need to write naming conventions in Upper case. An example is /INCLUDE

Sample Code of RPG

/Free
Read(E) TransFile; // Read file to prime do loop
DoW not %EOF(TransFile); // Continue until all processed
If %Error;
Dsply 'The read failed';
Leave;
Else;
Chain(N) CustNo CustMast CustData;
CustName = %Xlate(Upper : Lower : CustName);
ExSr CalcDividend;
Read(E) TransFile;
EndIf;
EndDo;

BegSr CalcDividend;
TotalSales = %XFoot(MthSales);
Eval(H) Dividend = TotalSales / 100 * DivPerc;
Record_transaction();
EndSr;

Declaration

dcl-Ds APIError lqualified;
   bytesprovided int(10) inz(%size(APIError));
   bytesavail    int(10) inz(0);
   msgid         char(7);
   *N            char(1);
   msgdata       char(240);
end-Ds;

Sub-routine Vs Sub-procedure

Subroutines are different from sub-procedures, but in a conceptual way they are often interchanged because they provide the same basic function in basic structured programming methodologies. Both provide a way to wrap re-usable code within a container.

AS 400 Language

Sub-Procedure Properties

  1. Parameters you can pass in Sub Procedures
  2. Returned value you can be used in somewhere else in a program
  3. Sub-procedures act as functions

Global Definitions

  • IMPORT and EXPORT are two key words you can use a Global variable across sub modules

Use of Sub-routines

  • Use of Sub-routines is very less in RPG. Mostly developers use Sub-procedures.
  • You can convert your sub-routines into Sub-procedures

Accessing Database

  1. You can access database objects in RPG
  2. You can embed SQL
  3. Easy to call Stored procedures
  4. Apply Triggers

References

Author: Srini

Experienced software developer. Skills in Development, Coding, Testing and Debugging. Good Data analytic skills (Data Warehousing and BI). Also skills in Mainframe.