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
**free
dsply 'Hello World';
return;
- 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.
- 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.
===> CALL yourlibrary/HELLO
"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
dsply name;
return;
dcl-s name char(10) inz('Jim');
dsply name;
return;
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.