Re: nested subroutines and scope [message #28979] |
Fri, 25 January 2002 10:40 |
Pavel A. Romashkin
Messages: 531 Registered: November 2000
|
Senior Member |
|
|
If variables need to be global within a procedure only, what's wrong
with passing them as parameters to another procedure called from the
parent one?
Pavel
Chris O'Dell wrote:
>
> This is a two-part question from an IDL newbie.
>
> 1) Is it possible to nest procedures or functions in IDL?
> 2) If the answer to (1) is yes, is there anyway to give certain
> variables global scope within the primary procedure/function,
> so they can be "seen" by all the nested procedures/functions
> within the main function?
>
> I used to do this all the time in pascal. Some pseudo-code might be
> as follows:
>
> function mainfunc, x, a1
>
> function subfunc, y
> return, a1 * y^2
> end ; subfunc
>
> return, subfunc(x) * subfunc(x^2)
>
> end ; mainfunc
>
> If you think this is terrible programming practice (which it probably
> is), can anyone suggest a better way, without having to pass
> all the global variables as parameters?
>
> Thanks,
> Chris O'Dell
|
|
|
Re: nested subroutines and scope [message #28985 is a reply to message #28979] |
Fri, 25 January 2002 08:36  |
David Burridge
Messages: 33 Registered: January 1998
|
Member |
|
|
Hi Chris,
Chris O'Dell wrote in message <3C50911E.3060801@cmb.physics.wisc.edu>...
> This is a two-part question from an IDL newbie.
>
> 1) Is it possible to nest procedures or functions in IDL?
> 2) If the answer to (1) is yes, is there anyway to give certain
> variables global scope within the primary procedure/function,
> so they can be "seen" by all the nested procedures/functions
> within the main function?
>
> I used to do this all the time in pascal. Some pseudo-code might be
> as follows:
>
> function mainfunc, x, a1
>
> function subfunc, y
> return, a1 * y^2
> end ; subfunc
>
> return, subfunc(x) * subfunc(x^2)
>
> end ; mainfunc
>
> If you think this is terrible programming practice (which it probably
> is), can anyone suggest a better way, without having to pass
> all the global variables as parameters?
>
> Thanks,
> Chris O'Dell
I *think* the answer to your first question is "no". Although you can
(obviously) call IDL procedures and functions from within other procedures
and functions, you cannot include a "PRO" or "FUNCTION" statement inside a
procedure/function.
As far as variable scope is concerned, the quick-and-dirty fix is to use a
common block(s) which makes it's contents visible globally. To do this, put
the common block name and contents of each routine that requires them.
However I don't like to use global variables because they are impossible to
keep track of and don't work if you have two copies of the same program
running at once. The alternative is, as you say, to pass the variables.
Hope this helps,
Dave
|
|
|