RPG Free Format
RPG Free Format
RPG Free Format
Version 1.0
Each free-form statement begins with an operation code and ends with a semicolon. Here is a list of
the new operation codes:
DCL-S salary
/IF DEFINED(large_vals)
PACKED(13:3)
/ELSE
PACKED(7:3)
/ENDIF
;
// no keywords
CTL-OPT;
// two keywords
CTL-OPT OPTION(∗SRCSTMT:∗NODEBUGIO)
DFTACTGRP(∗No);
D∗ fixed-form declarations
D string S 50A VARYING
D date S D DATFMT(∗MDY)
D obj S O CLASS(∗JAVA:'MyClass')
D ptr S ∗ PROCPTR
// free-form declarations
DCL-S string VARCHAR(50);
DCL-S date DATE(∗MDY);
DCL-S obj OBJECT(∗JAVA:'MyClass');
DCL-S ptr POINTER(∗PROC);
DCL-DS record_one;
buffer CHAR(25);
DCL-SUBF read INT(3);
END-DS;
D∗ fixed-form declaration
D myds DS
D subf1 11 15A
D subf2 5P 2 OVERLAY(myds)
D subf3 10A OVERLAY(myds:100)
D subf4 15A OVERLAY(myds:∗NEXT)
//free-form declaration
DCL-DS myds;
subf1 CHAR(5) POS(11);
subf2 PACKED(5:2) POS(1);
subf3 CHAR(10) POS(100);
subf4 CHAR(15);
END-DS;
DCL-PR proc_one;
buffer CHAR(25) CONST;
DCL-PARM read INT(3) VALUE;
END-PR;
READ
/FREE
ENDDO ENDDO;
/END-FREE
----------------------------------------------------------------------------------------------------------------------
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
IF %EQUAL IF %EQUAL;
ENDDO ENDDO;
ELSE ELSE;
ENDIF ENDIF;
/END-FREE
----------------------------------------------------------------------------------------------------------------------
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
ELSE ELSE;
ENDIF ENDIF;
/END-FREE
----------------------------------------------------------------------------------------------------------------------
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
IF %FOUND(Rickfile) IF %FOUND(Rickfile);
ELSE ELSE;
ENDIF ENDIF;
/END-FREE
----------------------------------------------------------------------------------------------------------------------
EVAL
Note: Some free format examples only work with V5R1 and higher.
/FREE
/END-FREE
----------------------------------------------------------------------------------------------------------------------
ELSEIF
/FREE
IF Age >= 20 and Sex = ‘F’ IF Age >= 20 and Sex = ‘F’;
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’;
ELSEIF Age >= 30 and Sex = ‘M’ ELSEIF Age >= 30 and Sex =‘M’;
ELSE ELSE
ENDIF ENDIF;
/END-FREE
----------------------------------------------------------------------------------------------------------------------
SELECT/ENDSL
/FREE
SELECT SELECT;
WHEN Age >= 20 and Sex = ‘F’ WHEN Age >= 20 and Sex = ‘F’;
WHEN Age >= 20 and Sex = ‘M’ WHEN Age >= 20 and Sex =‘M’;
WHEN Age >= 30 and Sex = ‘F’ WHEN Age >= 30 and Sex = ‘F’;
WHEN Age >= 20 and Sex = ‘M’ WHEN Age >= 30 and Sex =‘M’;
OTHER OTHER;
EVAL Code = 20 Code = 20;
ENDSL ENDSL;
/END-FREE
----------------------------------------------------------------------------------------------------------------------
Fixed Format Free Format
FOR
/FREE
ENDFOR ENDFOR;
/END-FREE
----------------------------------------------------------------------------------------------------------------------
%CHAR
Note: Converts the value of the expression from graphic, UCS-2, numeric, date, time,
/FREE
+ ‘ on ‘ + %CHAR(Date) + ‘ on ‘ + %CHAR(Date);
/END-FREE
----------------------------------------------------------------------------------------------------------------------
%EDITC
Note: Returns a character result representing the edited number after applying and edit code.
/FREE
+ TRIM(%EDITC(Amt * 12 + TRIM(%EDITC(Amt * 12
/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
/END-FREE
----------------------------------------------------------------------------------------------------------------------
SUBDUR
/FREE
/END-FREE
----------------------------------------------------------------------------------------------------------------------
EXTRCT
/FREE
/END-FREE
----------------------------------------------------------------------------------------------------------------------
Note: As of V5R2 you can convert character arguments to numeric with all of these functions.
/FREE
/END-FREE
***D Spec:
D ToDate S 8 0 INZ(20021231)
D WorkDate S D
Fixed Format
Free Format
/FREE
ToDate = 20021231;
WorkDate = %DATE(ToDate:*ISO); //WorkDate = D’2002-12-31’
/END-FREE
----------------------------------------------------------------------------------------------------------------------
%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.
/FREE
//Time = t’12.34.00’
/END-FREE
----------------------------------------------------------------------------------------------------------------------
%DIFF
Note: Used to find the duration between: two dates, two times, two timestamps, a date and the date
/FREE
DaysLeft = %DIFF(ExamDate:Today:*DAYS);
/END-FREE
----------------------------------------------------------------------------------------------------------------------
MONITOR/ENDMON
Note: Used for error handling routines. ON-ERROR 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
/FREE
MONITOR MONITOR;
ENDDO ENDDO;
ON-ERROR ON-ERROR;
***Handle all other errors**************************************
ENDMON ENDMON;
/END-FREE
----------------------------------------------------------------------------------------------------------------------
Fixed Format Free Format
LEAVESR
/FREE
LEAVESR;
/END-FREE
----------------------------------------------------------------------------------------------------------------------
OLD RPGIII:
/FREE
/END-FREE
----------------------------------------------------------------------------------------------------------------------
Note: Some languages refer to this as a Boolean data type. An indicator field is a single-byte 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
Examples follow:
D Spec:
D IsGood S N INZ(*OFF)
/FREE
IF IsGood IF IsGood;
ELSE ELSE;
ENDIF ENDIF;
/END-FREE
---------------------------------------------------------------------------------------------------------------------------
Free-form equivalents for keywords that are not supported in free-form definitions
---------------------------------------------------------------------------------------------------------------------------
Old Vs Free Format Logic