Solutions to exercises

Exercise 3-1

       dcl-s myCharfield char(5); 
myCharField = 'hello';
dsply myCharField;
return;

Back to Exercise 3-1

Exercise 3-2

The CHAR data type always has the same length, so the variable contains trailing blanks, which are used when the variable is concatenated with other text.

Bonus question: What RPG built-in function could be used in the concatenation expression to produce the same output in the version where the variable is defined with the CHAR type rather than the VARCHAR type?

Back to Exercise 3-2

Exercise 4-1

When the RETURN opcode is used, and *INLR is not on when the program returns, the RPG compiler does not implicitly close the file. The next time the program is called, the file is still open and still at end of file, so subsequent READ operations do not find any records and the loop exits immediately.

Bonus activity to correct the problem: Add a SETLL *START operation for the file, before the READ loop. That sets up the file so it is positioned at beginning of file, and the program works the same both times.

       setll *start rpgtestf;

Back to Exercise 4-1

Exercise 4-2

When the RETURN opcode is used, the RPG compiler does not implicitly close the file. The next time the program is called, the OPEN operation fails because the file already open.

Bonus activity to correct the problem: Add a check to see whether the file is already open before using the OPEN opcode.

       if not %open(rpgtestf);
open rpgtestf;
endif;
{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}
Pen
>