CL Programming Language Built-In Functions

Scott's Corner: Built-in CL Programming Functions

Joke of the month:

I just got fired from my job at the keyboard factory. They told me I wasn’t putting in enough shifts.

Tip of the month:

Are you taking advantage of the built-in functions offered in the CL programming language?  I was helping a colleague write a CL program recently, and I discovered that he didn’t know about all of the amazing functions IBM has added to CL over the past few years!

Most developers know that CL has always offered %SST (%SUBSTRING), %BIN (%BINARY) and %SWITCH. Many of them also know about %ADDR (%ADDRESS) and %OFS (%OFFSET) if they need to use pointers in CL.  But very few people seem to know about the other functions.

Here is a list of most of the CL built-in functions that I use, along with a description of when they were added to the language:

Function

Description

Release Added

%CHAR

Converts any variable to type(*char) format

7.2

%CHECK

Find the first position of string2 that isn’t a character in string1

7.1

%CHECKR

Find the last position of string2 that isn’t a character in string1

7.1

%DEC

Converts any variable to type(*dec) format.

7.2

%INT

Converts any variable to type(*int) format.

7.2

%UINT

Converts any variable to type(*uint) format.

7.2

%LEN

Calculates the length of a variable (in characters or digits)

7.2

%TRIML

Removes leading blanks from a character string

7.1

%TRIMR

Removes trailing blanks from a character string

7.1

%TRIM

Removes both leading and trailing blanks from a string

7.1

%LOWER

Convert text to all lowercase

7.2

%UPPER

Convert text to all uppercase

7.2

%SIZE

Calculate the size of a variable in bytes

7.1

%PARMS

Returns the number of parameters passed into this routine

7.3

%SCAN

Scan for a string within this string.

7.4

Unfortunately, I don’t have time to write about every parameter of every built-in function (there are even optional parameters that I won’t mention in this article.)   So I recommend checking the official documentation for details.

Here are some quick examples of these functions that will give you the gist.  They are very useful, I think they are great tools!

Check how many parameters were passed to a CL program:

Convert a numeric field to character:

Check for invalid characters in a character variable:

Determine the length of a variable after trailing blanks are removed:

Convert other data types (*CHAR, *LGL, etc) to numeric:

Get the number of digits in a numeric field, or the number of characters in a character field:

Get the size of a field in terms of the number of bytes of storage it occupies:

Scan for a character or substring in a string.  In this case, to help my users when they type IFS paths, I convert the windows-style slahes to the forward slashes used by most other operating systems with a simple loop:

Trim trailing blanks from a character string (without using *TCAT or *BCAT):

Trim leading blanks:

Trim both leading and trailing banks:

Convert a string to lowercase:

Convert a string to uppercase:

As you can see, there are a lot of uses for these functions!  They make string handling in CL almost as easy as other languages like RPG or Cobol!  Give them a try, they’re definitely going to make your job easier.

Scott Klement

Scott Klement
Midrange Dynamics Development & Solutions Architect

Scott Klement is an IT professional with a passion for both programming and mentoring. He joined Midrange Dynamics at the beginning of October 2022. He formerly was the Director of Product Development and Support at Profound 

Logic and the IT Manager and Senior Programmer at Klement’s Sausage Co., Inc. Scott also serves on the Board of Directors of COMMON, where he represents the Education, Innovation, and Certification teams. He is an IBM Champion for Power Systems.

Subscribe to our newsletter and join us next month to see what is happening in Scott’s Corner. Add a great dad joke to your arsenal and gain an even better IT insight from this recognized industry expert as he continues his quest to educate and support the IBM i community.

Similar Posts