October 10

0 comments

RPG Built-In Functions %BIF

By NickLitten

October 10, 2008

bif, ILE, RPG

When IBM introduced RPG IV (RPGLE) back in the mid 90’s, most of the RPG III opcodes were converted into built in functions. This makes for more readable code and efficient programming. The built in functions each do a particular function just like any opcode would have done. Since the built in functions are provided by the system itself, all the error handling etc is done by the system itself and you are given just the errors.

When we learn RPGLE procedures, we will see that the built in functions are very similar to procedures. Within the %BIF itself, ‘arguments’ (the important bits defining what the BIF is doing) are denoted by a colon. Here is an example:

// Look on a file called 'SOMEFILE" and if that key 'CompanyCode' is found then do something...
 Chain CompanyCode SOMEFILE
 If %Found(SOMEFILE)

Of course, we can get complex and use %BIFS for some really neat functions. All in one line of code this operation is saying – take the value #STRDATE which is *MDY format and convert it to *CYMD then make that date variable into a 7 long alpha character field before finally turning it into a 7,0 numeric value (which is how the dates are stored on the files in this program).

// convert screen dates from MMDDYY to 7,0 numeric in CYYMMDD format 
 =%dec(%char(%date(#StrDate:*mdy):*cymd0):7:0);

Some important built in functions are given in the list below.

RPGIV Built-in fucntion Action Performed

  • %Dec Converts a valid character value into a numeric value of specified size.
  • %Char Converts a numeric value into a character value.
  • %Date Create a date in the specified format. Return the current date if no parameter specified.
  • %LookUp Returns the position of a value in an array. If the search value is not found in the lookup array, 0 is returned.
  • %XLate Replaces characters of a string with predefined characters. Used mainly for case conversion of letters (From Capital to Small etc.)
  • %XFoot Returns the sum of all the array elements of an array
{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

Join the IBM i Community for FREE Presentations, Lessons, Hints and Tips

>