Built in Functions Free Format
Built in Functions Free Format
Format for RPGLE
Free Format for
RPGLE Search this site
RPG FREE FORMAT
COMPARED TO FIXED
FORMAT
RPG FREE FORMAT COMPARED TO
Sitemap
FIXED FORMAT
RPGIV/ILE/Free Format Operation Codes:
Some of the more useful opcodes follow, but not all.
Note: Some free format examples only work with V5R1 and higher.
Note: In free format end a line of code with a semicolon (;), all
code within structures are indented, and start a free format program
with /FREE
and end the program with /ENDFREE
Fixed Format Free
Format
READ
/FREE
EXCEPT Heading EXCEPT
Heading;
READ Rickfile READ
Rickfile;
DOW NOT %EOF DOW NOT
%EOF;
EXCEPT Detail EXCEPT
Detail;
READ Rickfile READ
Rickfile;
ENDDO ENDDO;
/ENDFREE
READE
Note: Some free format examples only work with V5R1 and higher.
Note: For free format always use the actual name of the file and not
the record format name.
/FREE
KEY SETLL Rickfile SETLL KEY
Rickfile;
IF %EQUAL IF %EQUAL;
KEY READE Rickfile READE
KEY Rickfile;
DOW NOT %EOF(Rickfile) DOW NOT
%EOF(Rickfile);
EXCEPT Detail EXCEPT
Detail;
https://sites.google.com/site/freeformatforrpgle/ 1/19
30/11/2015 Free Format for RPGLE
READ Rickfile READ
Rickfile;
ENDDO ENDDO;
ELSE ELSE;
EXCEPT NoDetail EXCEPT
NoDetail;
ENDIF ENDIF;
/ENDFREE
READP
Note: Some free format examples only work with V5R1 and higher.
Note: For free format always use the actual name of the file and not
the record format name.
Note: In free format the EVAL operation is not required sometimes.
/FREE
KEY SETGT Rickfile SETGT KEY
Rickfile;
READP Rickfile READP KEY
Rickfile;
IF NOT %EOF(Rickfile) IF NOT
%EOF(Rickfile);
EVAL Data# = Data# + 1 Data# =
Data# + 1;
ELSE ELSE;
EXSR Error EXSR
Error;
ENDIF ENDIF;
/ENDFREE
CHAIN
Note: Some free format examples only work with V5R1 and higher.
Note: For free format always use the actual name of the file and not
the record format name.
/FREE
KEY CHAIN Rickfile CHAIN KEY
Rickfile;
IF %FOUND(Rickfile) IF
%FOUND(Rickfile);
EXSR CustFound EXSR
CustFound;
ELSE ELSE;
EXSR NoCust EXSR
Error;
https://sites.google.com/site/freeformatforrpgle/ 2/19
30/11/2015 Free Format for RPGLE
ENDIF ENDIF;
/ENDFREE
Fixed Format Free
Format
EVAL
Note: Some free format examples only work with V5R1 and higher.
Note: In free format the EVAL operation is not required sometimes.
Multiple Examples follow:
/FREE
EVAL Total = A * (B – 1) Total = A * (B
– 1);
EVAL Total = Total +1 Total = Total
+ 1;
Total +=
1; //Short for previous example
EVAL Total = Total – Count Total = Total
– Count;
Total =
Count; //Short for previous example
EVAL Total = Total * Count Total = Total
* Count;
Total *=
Count; //Short for previous example
EVAL Total = Total / 7 Total = Total /
7;
Total /=
7; //Short for previous example
Total = Total
** 2; //Exponentiation
Total **=
2; //Exponentiation
for previous example
EVAL(H) Interest = Rate * Amt EVAL(H)
Interest = Rate * Amt; //EVALused for half adjust
EVAL A = %TRIMR(‘Hi ‘) + A =
%TRIMR(‘Hi ‘) + %TRIML(Chars);
%TRIML(Chars)
***After the EVAL below, the value of Chars contains ‘ab****ghijklmno’
https://sites.google.com/site/freeformatforrpgle/ 3/19
30/11/2015 Free Format for RPGLE
EVAL %SUBST(Chars:3:4) =
‘****’ %SUBST(Chars:3:4) = ’****’;
/ENDFREE
EVALR
Note: The expression is evaluation and the result is placed right
adjusted in the result field.
/FREE
EVALR Name = ‘Jimmie ‘ EVALR Name
= ‘Jimmie ‘;
//Name = ‘Jimmie ‘ //Name =
‘Jimmie ‘
EVALR Name = %TRIMR(‘Jimmie ‘) EVALR Name
= %TRIMR(‘Jimmie ‘;
//Name = ‘ Jimmie’ //Name =
‘ Jimmie’
/ENDFREE
Fixed Format Free
Format
ELSEIF
Note: Instead of Nesting Ifs use an ELSEIF or a SELECT/ENDSL
below.
In the last stage of the ELSEIF, the ‘ELSE’ part is optional.
/FREE
IF Age >= 20 and Sex = ‘F’ IF Age >= 20
and Sex = ‘F’;
EVAL Code = 5 Code = 5;
ELSEIF Age >= 20 and Sex = ‘M’ ELSEIF Age
>= 20 and Sex = ‘M’;
EVAL Code = 4 Code = 4;
ELSEIF Age >= 30 and Sex = ‘F’ ELSEIF Age
>= 30 and Sex = ‘F’;
EVAL Code = 10 Code = 10;
ELSEIF Age >= 30 and Sex = ‘M’ ELSEIF Age
>= 30 and Sex = ‘M’;
https://sites.google.com/site/freeformatforrpgle/ 4/19
30/11/2015 Free Format for RPGLE
EVAL Code = 9 Code = 9;
ELSE ELSE
EVAL Code = 20 Code = 20;
ENDIF ENDIF;
/ENDFREE
SELECT/ENDSL
Note: Instead of Nesting Ifs use a SELECT/ENDSL or an ELSEIF
above.
In the last stage of the SELECT, the ‘OTHER’ part is optional.
/FREE
SELECT SELECT;
WHEN Age >= 20 and Sex = ‘F’ WHEN Age >=
20 and Sex = ‘F’;
EVAL Code = 5 Code = 5;
WHEN Age >= 20 and Sex = ‘M’ WHEN Age >=
20 and Sex = ‘M’;
EVAL Code = 4 Code = 4;
WHEN Age >= 30 and Sex = ‘F’ WHEN Age >=
30 and Sex = ‘F’;
EVAL Code = 10 Code = 10;
WHEN Age >= 20 and Sex = ‘M’ WHEN Age >=
30 and Sex = ‘M’;
EVAL Code = 9 Code = 9;
OTHER OTHER;
EVAL Code = 20 Code = 20;
ENDSL ENDSL;
/ENDFREE
Fixed Format Free Format
FOR
Note: Controls the number of times a group of operations are
processed.
/FREE
EVAL Factor = 1 Factor = 1;
FOR I = 1 to %LEN(Field) FOR I = 1 to
https://sites.google.com/site/freeformatforrpgle/ 5/19
30/11/2015 Free Format for RPGLE
%LEN(Field);
EVAL Factor = Factor + 1 Factor = Factor
+ 1;
ENDFOR ENDFOR;
/ENDFREE
%CHAR
Note: Converts the value of the expression from graphic, UCS2,
numeric, date, time,
or timestamp data to type character.
/FREE
EVAL Result = ‘It is ‘ + %CHAR(Time) Result = ‘It is ‘
+%CHAR(Time)
+ ‘ on ‘ + %CHAR(Date) + ‘ on ‘ +
%CHAR(Date);
*** Result = ‘It is 12:23:34 on 02/02/1977’
/ENDFREE
%EDITC
Note: Returns a character result representing the edited number after
applying and edit code.
/FREE
EVAL Sal = ‘The annual salary is ‘ Sal = ‘The annual
salary is ‘
+ TRIM(%EDITC(Amt * 12 +
TRIM(%EDITC(Amt * 12
:’A’ : *CURSYM)) :’A’:
*CURSYM));
*** Sal = ‘The annual salary is $12,000.00’
/END
FREE
ADDDUR
Note: The ADDDUR operation adds the duration specified in factor 2
to a date or time and places the resulting Date, Time or Timestamp in
the result field.
/FREE
BillDate ADDDUR 30:*DAYS DueDate DueDate =
BillDate + %DAYS(30);
https://sites.google.com/site/freeformatforrpgle/ 6/19
30/11/2015 Free Format for RPGLE
/ENDFREE
SUBDUR
Note: Subtract a duration to establish a new Date, Time or
Timestamp.
/FREE
DueDate SUBDUR 30:*DAYS BillDate BillDate =
DueDate %DAYS(30);
/ENDFREE
Fixed Format Free Format
EXTRCT
Note: Extracts a portion of a date, time, or timestamp data item.
/FREE
EXTRCT BirthDate:*Y BirthYear BirthYear =
%SUBDT(BirthDate:*YEARS);
/ENDFREE
%DEC / %DECH / %UNS / %UNSH / %INT /
%INTH / %FLOAT
Note: As of V5R2 you can convert character arguments to numeric
with all of these functions.
Note: These functions can now be used to support “LEGACY” code.
Note: The sign (+ or ), decimal point (. or ,), are optional.
Note: Invalid numeric data will set %STATUS = 00105.
/FREE
EVAL Chars = ‘123.56’ Chars = ‘123.56’;
EVAL Num = %DEC(Chars:5:2) Num =
%DEC(Chars:5:2); //Num = 123.56
EVAL Num = %UNSH(Chars) Num =
%UNSH(Chars); //Num = 124
/ENDFREE
***D Spec:
D ToDate S 8 0 INZ(20021231)
D WorkDate S D
Fixed Format
EVAL ToDate = 20021231
EVAL WorkDate =
https://sites.google.com/site/freeformatforrpgle/ 7/19
30/11/2015 Free Format for RPGLE
%DATE(ToDate:*ISO) //WorkDate = D’20021231’
EVAL WorkDate = WorkDate +
%DAYS(30) //Add 30 days to WorkDate
EVAL ToDate =
%UNS(%CHAR(WorkDate:*ISO)) //ToDate = 20030130
Free Format
/FREE
ToDate = 20021231;
WorkDate =
%DATE(ToDate:*ISO); //WorkDate = D’2002
1231’
WorkDate = WorkDate +
%DAYS(30); //Add 30 days to WorkDate
ToDate =
%UNS(%CHAR(WorkDate:*ISO)); //ToDate =
20030130
/ENDFREE
Fixed Format Free Format
%TIME / %TIMESTAMP
Note: Converts the value of the expression from character, numeric,
or |timestamp data to type time. The converted value remains
unchanged, but |is returned as a time.
Note: Available as of V5R2.
/FREE
EVAL String = ’12:34 PM’ String = ’12:34
PM’;
EVAL Time = %TIME(String,*USA) Time =
%TIME(String:*USA);
//Time =
t’12.34.00’
/ENDFREE
Fixed Format Free Format
%DIFF
Note: Used to find the duration between: two dates, two times, two
timestamps, a date and the date
https://sites.google.com/site/freeformatforrpgle/ 8/19
30/11/2015 Free Format for RPGLE
portion of a timestamp, and a time and the time portion of a
timestamp.
/FREE
EVAL DaysLeft = %DIFF(ExamDate: DaysLeft =
%DIFF(ExamDate:Today:*DAYS);
Today:*DAYS) /ENDFREE
MONITOR/ENDMON
Note: May be used with V5R1 and later.
Note: Used for error handling routines. ONERROR list one or more
errors for which it is responsible.
These errors generally correspond to the %STATUS code from 00100
to 09999 or you can use *FILE
for file errors.
/FREE
MONITOR MONITOR;
READ Rickfile READ Rickfile;
DOW NOT %EOF(Rickfile) DOW NOT
%EOF(Rickfile);
KEY CHAIN Rickfile2 CHAIN KEY
Rickfile2;
EXSR DoSomething EXSR
DoSomething;
READ Rickfile READ
Rickfile;
ENDDO ENDDO;
ONERROR 1211 ONERROR
1211;
EXSR NotOpen EXSR NotOpen;
ONERROR 1218 ONERROR
1218;
EXSR LockedRec EXSR
LockedRec;
ONERROR *FILE ONERROR
*FILE;
EXSR FileErr EXSR FileErr;
ONERROR 00100 : 00121 ONERROR
00100 : 00121;
***Handle string error and arrayindex
error***********************
EXSR STRARY EXSR
STRARY;
https://sites.google.com/site/freeformatforrpgle/ 9/19
30/11/2015 Free Format for RPGLE
ONERROR ONERROR;
***Handle all other
errors**************************************
EXSR GenErr EXSR GenErr;
ENDMON ENDMON;
/ENDFREE
Fixed Format Free Format
LEAVESR
Note: Used with free format only. Leaves a subroutine.
/FREE
LEAVESR;
/ENDFREE
MOVE AND MOVEL
Note: RPGIV and free format provides a smoother process.
OLD RPGIII:
MOVEL FIELD1 FIELDA
MOVE FIELD2 FIELDA
MOVEL FIELDA FIELDB
MOVE FIELD3 FIELDB
NEW RPGIV and free format:
/FREE
EVAL FieldB = Field1 + Field2 + Field3 FieldB
= Field1 + Field2 + Field3;
/END
FREE
INDICATOR DATA TYPE
Note: Some free format examples only work with V5R1 and higher.
Note: Some languages refer to this as a Boolean data type. An
indicator field is a singlebyte field
that can contain only two logical values: ‘1’ or ‘0’. You can also refer
to these values using *ON
and *OFF, respectfully. Indicator data is usually used within an
RPGIV program to signal a true/false
condition and can be tested on as a true/false condition.
Examples follow:
D Spec:
D IsGood S N INZ(*OFF)
https://sites.google.com/site/freeformatforrpgle/ 10/19
30/11/2015 Free Format for RPGLE
/FREE
EXSR ChkGood EXSR
ChkGood;
IF IsGood IF IsGood;
EXSR DoSomething EXSR
DoSomthing;
EVAL IsGood = *OFF IsGood
= *OFF;
ELSE ELSE;
EXSR GiveErr EXSR
GiveErr;
ENDIF ENDIF;
/END
FREE
AIDGenerating Keys(For testing if a
key has been pressed)
The AID (attention indicator) code identifies to the host system the
Traducir
function being requested from the keyboard. The AID code is
returned by certain input operations when the operator presses an
AIDgenerating key. The following table lists the AIDgenerating
keys and the AID codes associated with each key for INFDS.
Note: If you don’t like testing with indicators, try using the INFDS
Note: Try using the File Information Data Structure (INFDS) below:
Note: Example code that is less cryptic than using indicators is on the next
page.
F Spec:
F RickDsply CF E WORKSTN
INFDS(WkdSpa)
***File Information Data Structure
D Specs:
D WKDSPA DS
D Fkey 369 369
***Only use the keys you need below in your code
***Keys Pressed Constants
D F1 C CONST(X’31’)
D F2 C CONST(X’32’)
https://sites.google.com/site/freeformatforrpgle/ 11/19
30/11/2015 Free Format for RPGLE
D F3 C CONST(X’33’)
D F4 C CONST(X’34’)
D F5 C CONST(X’35’)
D F6 C CONST(X’36’)
D F7 C CONST(X’37’)
D F8 C CONST(X’38’)
D F9 C CONST(X’39’)
D F10 C CONST(X’3A’)
D F11 C CONST(X’3B’)
D F12 C CONST(X’3C’)
D F13 C CONST(X’B1’)
D F14 C CONST(X’B2’)
D F15 C CONST(X’B3’)
D F16 C CONST(X’B4’)
D F17 C CONST(X’B5’)
D F18 C CONST(X’B6’)
D F19 C CONST(X’B7’)
D F20 C CONST(X’B8’)
D F21 C CONST(X’B9’)
D F22 C CONST(X’BA’)
D F23 C CONST(X’BB’)
D F24 C CONST(X’BC’)
D CLEAR C CONST(X’BD’)
D ENTER C CONST(X’F1’)
D HELP C CONST(X’F3’)
D PAGEUP C CONST(X’F4’)
D PAGEDN C CONST(X’F5’)
D PRINT C CONST(X’F6’)
Fixed Format Free
Format
/FREE
IF Fkey = F1 IF Fkey =
F1;
EXSR Func1 EXSR
Func1;
ELSEIF Fkey = F2 ELSEIF
Fkey = F2;
EXSR Func2 EXSR
Func2;
ELSEIF Fkey = Enter ELSEIF
https://sites.google.com/site/freeformatforrpgle/ 12/19
30/11/2015 Free Format for RPGLE
Fkey = Enter;
EXSR FuncEnter EXSR
FuncEnter;
ELSEIF Fkey =
PageUp ELSEIF Fkey = PageUp;
EXSR FuncPgUp EXSR
FuncPgUp;
ELSEIF Fkey = PageDn ELSEIF
Fkey = PageDn;
EXSR FuncPgDn EXSR
FuncPgDn;
ENDIF ENDIF;
/END
FREE
OR
Note: If you don’t like testing with indicators, try using the INDDS keyword
which lets you associate a data structure name with the INDARA indicators for a
workstation or printer file. This data structure contains the conditioning and
response indicators passed to and from data management for the file, and is
called an indicator data structure.
Note: Example code that is less cryptic than using indicators is on the next
page.
F Spec:
F RickDsply CF E WORKSTN
INDDS(Indicators)
Example code on next page.
* These indicator variables can be tested on as Boolean Logical Variables.
* ‘IF Exit’ means the same thing as ‘IF Exit = *ON’.
In other words you test by coding ‘IF Exit’ and don’t use ‘If Exit = *ON’ and
code ‘IF NOT Exit’ and don’t use ‘IF Exit = *OFF’.
* To use these indicator types you have to declare the keyword INDARA in you
display file (DDS).
D Indicators DS
* For PageDown(Rename PageDown to indicator 21 in subfile definition:
PAGEDOWN(21 ‘PageDown’)
D PagDwn 21 21N
* Use for F3=Exit(Rename CF03 to indicator 03 in subfile definition: CF03(03 ‘F3=Exit’)
D Exit 3 3N
* Use for F5=Update(Rename CF05 to indicator 05 in subfile definition: CF05(05
‘F5=Update’)
D Update 5 5N
* Use for F12=Cancel(Rename CF12 to indicator 12 in subfile definition: CF12(12
‘F12=Cancel’)
D Cancel 12 12N
https://sites.google.com/site/freeformatforrpgle/ 13/19
30/11/2015 Free Format for RPGLE
* Conditioning indicators for format "DispSflCtl"
D SflDSpctl 41 41N
D SflDsp 42 42N
D SflEnd 43 43N
D SflClr 44 44N
* Set indicators to display the subfile:
Fixed Format Free
Format
C /FREE
C EVAL SflDsp = *ON SflDsp =
*ON;
C EVAL SflEnd = *OFF SflEnd =
*OFF;
C EVAL SflClr = *OFF SflClr =
*OFF;
C EXFMT DispSflCtl EXFMT
DispSflCtl;
*
* Using indicator variables, we can write more readable programs:
C EXFMT Query EXFMT
Query;
C IF Exit or Cancel IF Exit or
Cancel;
C RETURN RETURN;
C ELSEIF Update ELSEIF
Update;
C EXSR Refresh EXSR
Refresh;
C ELSEIF PagDwn ELSEIF
PagDwn;
C EXSR LoadSF EXSR
LoadSF;
C ENDIF ENDIF;
C /ENDFREE
BUILTIN FUNCTIONS
The original release of RPG IV included a set of builtin functions. These built
in functions were:
%ADDR, %PADDR, %SIZE, %ELEM, %SUBST, %TRIM, %TRIML, %TRIMR
In addition, under OS/400 V3R2 and V3R7 the %PARMS builtin function was
introduced. Since then, several builtin functions have been added to RPG IV.
The following table provides the OS/400 Version and Release that the specific
builtin functions were introduced and/or enhanced.
https://sites.google.com/site/freeformatforrpgle/ 14/19
30/11/2015 Free Format for RPGLE
https://sites.google.com/site/freeformatforrpgle/ 15/19
30/11/2015 Free Format for RPGLE
https://sites.google.com/site/freeformatforrpgle/ 16/19
30/11/2015 Free Format for RPGLE
V5R1 *ON if the job is being shut
https://sites.google.com/site/freeformatforrpgle/ 17/19
30/11/2015 Free Format for RPGLE
%SHTDN PWRDWNSYS command is
*OFF is returned.
https://sites.google.com/site/freeformatforrpgle/ 18/19
30/11/2015 Free Format for RPGLE
INTERNET ADDRESSES FOR SOURCES OF INFORMATION ON RPGIV
AND FREE FORMAT
www.iseries.ibm.com
www.redbooks.ibm.com
www.search400.com
www.rpgiv.com
www.mcpressonline.com
www.iseriesnetwork.com
Home page for all versions of RPG releases:
http://publib.boulder.ibm.com/pubs/html/as400/online/homeeng1.htm
https://sites.google.com/site/freeformatforrpgle/ 19/19