Wednesday, January 31, 2007

 

The Long and Short of Subprocedure Return Values


User-defined functions in RPG, especially in the context of free-form calculations, can simplify programming tasks (and make calculation specifications more fun to write). However, you should keep in mind that return values are implicitly returned by value. Therefore, the same considerations that apply to VALUE parameters also apply to return values (see prior post on this subject).


In the general case, a value returned from a subprocedure is copied twice. The first copy occurs when the RETURN operation is evaluated, which copies the function's return value to a temporary location. The second copy occurs back at the call site, where the function result is copied from the temporary location to a variable in the caller's storage. If the return value is a varying-length data type, the temporary location is the full (declared) length of the varying length result.


So, it is important from a performance and storage perspective to strike some balance in the length of a character return value. You need to determine some practical limit to the size returned by your procedures.


As an alternative to returning a varying-length character string as a subprocedure return value, the programmer should consider returning the value in a parameter that is passed by reference (that is, an output parameter). That will reduce the storage requirements and eliminate one copy of the returned data, which can be significant for larger lengths (like 32K or more).


Comments: Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?