From: jia on
Hello all:

I have to call many functions in a single script. Almost all the functions will use a particular variable (e.g. num ) as an input.

Normally, I can sepcify an input argument named by num for all the function, e.g,
function1(.., . , num)
function2(..., ..., num)
function3(..., .., num)

I hope to simplify the above method by defining a global variable in the script. i.e,
global num

Then I will not cite the num variable as an input argument, e.g.,
function1(..., ...,) %% without the global variable, num
function2(..., ...,) %% without the global variable, num
function3(..., ...,) %% without the global variable, num

But I tried and find the above method seems not correct. So is that any way that I can avoid set the global variable (e.g. num) to an nput argument of the function though this global variable will be still used in the function?
From: Walter Roberson on
jia wrote:


> I have to call many functions in a single script. Almost all the
> functions will use a particular variable (e.g. num ) as an input.
> Normally, I can sepcify an input argument named by num for all the
> function, e.g,
> function1(.., . , num)
> function2(..., ..., num)
> function3(..., .., num)
>
> I hope to simplify the above method by defining a global variable in the
> script. i.e,
> global num
>
> Then I will not cite the num variable as an input argument, e.g.,
> function1(..., ...,) %% without the global variable, num
> function2(..., ...,) %% without the global variable, num
> function3(..., ...,) %% without the global variable, num
>
> But I tried and find the above method seems not correct. So is that any
> way that I can avoid set the global variable (e.g. num) to an nput
> argument of the function though this global variable will be still used
> in the function?

A variable must be declared using the "global" statement in _every_ routine
that wishes to access the variable.

You should consider using nested functions and a shared variable.