PMS Information Systems
Welcome to PMS Information Systems - IBM i (AS/400) Forum !!!

Get Answers for all your queries on IBM i (AS/400).

A RECURSIVE CALL: PROGRAM VS SUBROUTINE VS SUBPROCEDURE

View previous topic View next topic Go down

A RECURSIVE CALL: PROGRAM VS SUBROUTINE VS SUBPROCEDURE

Post  maran on Fri Dec 30, 2011 1:18 pm

A recursive call is generated when a subprocedure calls itself. You cannot accomplish this in any other way using RPG. For instance, you cannot call PROGRAMA from within PROGRAMA. Nor can you call PROGRAMA, which calls PROGRAMB, which then again calls PROGRAMA. These are no-no's and a nasty run-time error or, worse, an angry user will tell you so.

The same holds true for subroutines. You cannot call a subroutine from within itself. The good news here is that the compiler won't even let you code a subroutine that calls itself. Big deal, right? Subprocedures support recursion.

So when is the right time to use recursion? How about if you needed to calculate factorials? You know, 5 factorial (5!) is 5x4x3x2x1, or 120. Computing factorials may be fine for a programming assignment in school, but you are probably interested in a real-life business reason to employ recursive calls. The best time to take advantage of recursion is when your data is stored in a hierarchical level as opposed to the relational level to which we are accustomed. In a relational database, you have a parent-child relationship. That is, you have a master file in a one-to-many relationship with detail records contained in a detail file. RPG programmers are adept at displaying or printing information stored in a relational database structure. A hierarchical structure (also known as a recursive structure) does not have this parent-child relationship. In this hierarchical structure, the data is independent of the rest of the data while also possibly being associated with it.
avatar
maran
Admin

Posts : 442
Join date : 2009-07-24

View user profile http://pmsinformationsystem.forumotion.net

Back to top Go down

View previous topic View next topic Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum