× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



David

Think of this scenario -

Let's say your procedure receives a number and adds it to a subtotal. The subtotal is your static variable. Your procedure adds the passed value to the subtotal and returns.

Now let's say you need to start a new subtotal? How do you do this? Well, you can't from the outside, directly, because the static variable is local to the procedure - it is not visible outside of the procedure. So you have to tell the procedure to initialize it.

RCLRSC is definitely NOT the way to do this - it might have worked in OPM and would still do, but it is not my idea of good programming technique.

I can get mixed up in this stuff, too, so it is always worthwhile to review things like scope and lifetime and visibility where variables are concerned. Those are terms in common usage, I believe. Here's what I understand about things in RPG, and, I believe, Pascal.

Global variables are static - they exist for the life of the module call and keep their values between calls (unless LR is set where that matters). They are visible throughout the module, including procedures, unless hidden by a variable of the same name in a procedure.
Local variables (not static) are automatic - they exist only so long as the procedure is active. They are reinitialized the next time the procedure is called. They are visible only within the procedure - nothing outside the procedure can see them, therefore, cannot modify them.
Local variables (static) are static - they exist for the life of the procedure call and keep their values between calls. They are not reinitialized the next time the procedure is called. They are visible only within the procedure - nothing outside the procedure can see them, therefore, cannot modify them.

As to returning without performing the normal task - no, I would not do that - I would say, do what is done the first time - initialize the static value and process it in whatever way is done. If building a subtotal, add the passed value to 0.

HTH
Vern

David FOXWELL wrote:
-----Message d'origine-----
De : rpg400-l-bounces@xxxxxxxxxxxx [mailto:rpg400-l-bounces@xxxxxxxxxxxx] De la part de Birgitta Hauser
In this situations I use an optional parameter (simply an indicator). If this parameter is passed, the static variables first get initialized.
At the first call, I pass the optional parameter (with *ON) and for all subsequent the parameter is either not passed or passed with *OFF.

Hi Birgitta,

Does that mean you have to call all your subprocedures that contain static variables and include code in each subprocedure so that it behaves differently, ie, it initializes the variables then returns without performing its normal task?

As an Amazon Associate we earn from qualifying purchases.

This thread ...

Follow-Ups:
Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.