Programming in RPG / Free – A Beginner’s Tutorial

This is a mirror of a wonderful article from RPG Cafe!
(Short URL: https://ibm.biz/rpgCafe_Freeform_Rpg_Tutorial)

Coding in RPG IV - a beginner's tutorial

This tutorial is intended for experienced programmers who want to learn RPG IV (also known as ILE RPG).

This tutorial assumes.

  • That you have IBM i 7.1 or later so that you can use the free-form version of most RPG IV statement types. (There may be some coding examples that require later releases. If so, this will be noted with the example. But 7.1 will be sufficient for most of this tutorial.)
  • That you have no prior knowledge of RPG III.
  • That you have some prior knowledge of IBM i (also known as AS/400, iSeries, System i).
  • That you know where to find the manuals for RPG in the IBM Knowledge Center.

The coding examples are intended to show the feature being discussed, but they sometimes have a "Bonus features for this example" section following the example that lists additional interesting things to note about the example, usually things that have not been discussed yet. Readers can choose to ignore those "Bonus features" sections without worrying about missing something, since the material is covered in later chapters.

Chapter 1: Hello world

This chapter takes you through the steps to create and run an RPG IV program.

It assumes you are using one of two ways to do your coding:

  • RDi
  • PDM and SEU through some type of 5250 emulator

Initial setup

If you are using RDi:

  • Create a connection to your IBM i.
  • Create a library. Remember the name of the library you create. You need it later when you call your first program. (Replace yourlibrary in the instructions with the name of the library you created.)
  • Create a source file.
  • Create a source member in the file called HELLO.RPGLE.
  • Open the member in the editor.

If you are using SEU:

  • Log on to a session on your IBM i.
  • Create a library by using the CRTLIB command.
  • Create a source file by using the CRTSRCPF command.
  • WRKMBRPDM specifying your library and source file.
  • Create a source member HELLO in the file by using F6 in WRKMBRPDM or by using the ADDPFM command, giving it type RPGLE.
  • Open the member in SEU by using the "2" option.
  • Warning: SEU gives errors for every free-form H, F, D, or P statement. The RPG syntax checker used by SEU was last updated in 6.1, so it does not understand the new syntax. If you still want to use SEU, you have to exit with errors after you save your source.

Entering the RPG program statements in "fully-free" form

  • Type the following code. Be sure that the first **FREE goes in column 1 of line 1. If you are using RDi, the editor shows you all the columns. If you are using SEU, the editor does not show you columns 1-5 by default, so the first column you see is column 6. Use F19 to shift the code left so you can see column 1.
    **free
    dsply 'Hello World';
    return;
  • Compile the program
    • If you are using RDi, you can compile from the menu within the editor, or you can save your changes and compile by right-clicking on the member in the navigation. Either way, select the CRTBNDRPG command, and take all the defaults.
    • If you are using SEU, exit the editor by using F3, and compile by using option 14. You could also compile from the command line by using the CRTBNDRPG command, and by using F4 to prompt the command.
  • If you are using RDi, log on to a session on the IBM i by using an emulator
    • Do the following command.

      ===> WRKOBJPDM yourlibrary

      (The WRKOBJPDM command puts you into a list of the objects in that library. You don't actually need to do this step to call your program, but because of an oddity in how the system determines whether to halt your job to show you a simple DSPLY message, it's convenient to get into a screen where the job halts. If you decide not to use WRKOBJPDM, or if it's not available, then your program just ends without letting you see the DSPLY. In that case, use DSPJOBLOG, then F10, then F6, to see the output.)

    • If you are using SEU, exit the editor by using F3, and compile by using option 14. You could also compile from the command line by using the CRTBNDRPG command, and by using F4 to prompt the command.
  • (If you are using PDM, you are already logged on.)
  • Use the CALL command to call the HELLO program, substituting the name of your library for yourlibrary.

    ===> CALL yourlibrary/HELLO

  • It puts up a message saying "DSPLY Hello World"
  • Just press ENTER to end the program.
  • "Fully free form" vs "column-limited free form"

    RPG has two modes of free-form code. The historical mode (column-limited mode) requires that all free-form code must be coded between columns 8 and 80. Fully-free mode allows free-form code in any column, with no limit on line-length.

    Fully free-form source must have **FREE in column 1 of line 1. All the code must be free-form.

    Non-fully-free source, or column-limited source does not have **FREE in line 1. You can mix fixed-form code (code that uses columns 6 and 7) and free-form code in column-limited source.

    This tutorial assumes you have **FREE at the beginning of your source, but it also works if you use column-limited source, if you ensure that columns 1-7 are blank and that you don't go past column 80. The examples have 7 blanks at the beginning to make it easy to copy and paste into column-limited source, but if you have **FREE at the beginning of your source, you can remove those blanks if you like.

    Dealing with compiler errors

  • Edit your source again, and change it so instead of displaying 'Hello World', it tries to display a variable called "name". This is an undefined variable and generates a compiler error.
           dsply name;
    return;
  • Delete the old version of the program by using the DLTPGM command.
  • Compile the program again. It fails with a severity 30 error.
  • If you are using RDi, click the RNF7030 error in the Error List Window. It highlights the line that tried to display the "name" variable.
  • If you are using SEU, use the WRKSPLF command and then use F18 to get to the end of the list of spooled files. Use option 5 on the HELLO spooled file and use the B command to go to the end of the listing. Page up a bit until you find the list of the error messages, then take note of the error message with the highest severity (the RNF7030 message). Return to the beginning of the file and enter RNF7030 on the search line, then hit F16 to locate the error message in the listing. The error message has a statement number (statement 2). Page back in the listing to find statement 2 (on the left side of the listing). It is the line that tried to display the "name" variable.
  • The error message indicates that NAME is not defined. To correct it, you have to add a definition for a variable called NAME. (Note that RPG IV is not case-sensitive for variable names, so variable "name" is the same as variable "NAME". The uppercase form of the name appears in the listing in error messages and the cross-reference.)
  • Add the following dcl-s statement to the beginning of your program to define variable "name". Remember to code only between columns 8 - 80.
           dcl-s name char(10) inz('Jim');

    dsply name;
    return;
  • Recompile the program and call it again. It puts up a message saying "DSPLY Jim".
  • Debugging

    Compile your program again, specifying a debug view other than *NONE or *STMT. For example, specify DBGVIEW(*LIST). (But *ALL, *SOURCE, or *COPY would all work fine here.)

    Now, start the debugger. For now, I'll just mention the system debugger, but there is a more intuitive debugger associated with RDi that you can investigate separately.

    Assuming your program is called QTEMP/MYPGM, do the following commands:

    ===> STRDBG QTEMP/MYPGM    - at this point, you can just use F10 to exit the screen    - when you call the program, it stops on the first      executable statement    - or you could also use F6 to set a breakpoint on a      particular statement ===> CALL QTEMP/MYPGM    - when you get a breakpoint, step (F10) through the program    - display variables (F11) along the way ===> ENDDBG

    That's it!

    From now on, I'll assume you know

    • How to edit and compile your code.
    • How to locate the error messages associated with a failed compile.
    • How to call a program.
    • How to debug a program.
    {"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}
    Pen
    >