Recursion

Hello Friends.

HAPPY NEW YEAR! Happiness, Health, Peace and Prosperity by the Grace of God!

I came across a subprogram that calls another subprogram that calls the first one (am not sure if it ever happens).

My question is: is such code permitted in Natural ?

If the first subprogram is actually called by the second one, is another storage area created for this recursive call OR does the recursive call use the same storage areas ?

Thanks so much for your help.

Yes, you can code like this; no problem.

Yes, Natural creates a new Local Data Area each time a subprogram is invoked.

steve

Thanks very much, Steve …

Yes that works and is handled no different than any other call to a subprogram, but real recursion in Natural is done when you have an external subroutine that calls itself. In 20+ years of Natural coding, I’ve only done it once, but it worked like a charm.

So, MOD-A callnats MOD-B which callnats MOD-A.

The data storage will be distinct as long as the modules are not sharing parameters. If you calls look like this:

*** PGM-1
DEFINE DATA LOCAL
1 #X (A7)
...
CALLNAT 'MOD-A' #X
...
*** MOD-A:
DEFINE DATA PARAMETER
1 #A   (A7)
...
CALLNAT 'MOD-B' #A
...
*** MOD-B:
DEFINE DATA PARAMETER
1 #B   (A7)
...
CALLNAT 'MOD-A' #B
...

Then #X from PGM-1, #A from MOD-A, #B from MOD-B and #A from the second instance of MOD-A all share the same storage location. And that storage is allocated in the DEFINE DATA LOCAL from PGM-1.

The only limitation I know is about nesting of copycodes. See NAT0475. However, it doesn’t make sence to include copycodes recursively…

Other than the storage issue mentioned whereby you need to be sure you are using distinct parameter areas or the same parameter data area depending on how you view data from the initial call being changed by the send call and returned to the primary initiating process, there is one other thing to be careful of and that is infinite recursiveness. :evil:

Be careful to ensure that there is no way that when a subprogram calls itself that it does not call itself again and again and again ad infinitum. :roll:

This is indeed not necessary, as an infinite recursion is not possible in Natural: the nesting level is limited, at least by the system variable *LEVEL (N2), so nesting levels above 99 are not possible. However, infinite program calls in loops may be possible, i.e. FETCHes (without RETURN) which do not increase *LEVEL.

This is indeed not possible as there is no conditional compilation in Natural (to terminate the recursion). :wink:

[quote="Wilfried B

with Natural 4.2.1 on mainframe z/OS, real limit is 98 times - NAT0921 is thrown on 99th callnat (99th “ADD” is not executed). whether or not *LEVEL is displayed.

So we have another (unimportant?) difference/incompatibility. I do no longer wonder why Natural is going down!

Imagine the fillowing scenario: an experienced mainframe Natural programmer turns to OpenSystems Natural and explains his employer, that the recursion depths in Natural is limited to 98 as *level is N2. Then a less experienced Natural programmer says: No, it is limited to 512! Now, who do you think is the one who is blamed?

In my opinion there is no need to have these differences in the different Natural versions. SAG should avoid differences where ever possible!

Just my opinion.

I put in an Enhancement Proposal #4777. If you have access to Servline24, you can view it there and add your comments. Adding your comments helps a proposal, even if you just indicate your support for the idea.

http://servline24.softwareag.com/SecuredServices/RequestManagement/NewRQM/ModifyEP/modEPfrm.asp?EP=4777