From: bjorn.tor.larsson on
As far as I understand, in order to make variables global, they need to be stated with "global variablename" in each and every function where they should be available. Is there another way?

Maybe I have a poor programming style, but I need lots of variables available in lots of functions. Most of my coding time is spent on adding variable names to those global lists in all functions. It can't be meant to be like this.

Thanks for any help beforehand!
From: Nic Roberts on
Unfortunatly I dont think there is another way. Personally I avoid using globals if at all possible. If your code isnt parallel then it should be possible to pass variables in and out of functions from your main code without need for global variables.

Nic

"bjorn.tor.larsson(a)gmail.com" <bjorn.tor.larsson(a)gmail.com> wrote in message <95126884.41137.1279877740041.JavaMail.root(a)gallium.mathforum.org>...
> As far as I understand, in order to make variables global, they need to be stated with "global variablename" in each and every function where they should be available. Is there another way?
>
> Maybe I have a poor programming style, but I need lots of variables available in lots of functions. Most of my coding time is spent on adding variable names to those global lists in all functions. It can't be meant to be like this.
>
> Thanks for any help beforehand!
From: BjornL on
Thanks for you quick answer! Too bad one can't globalize variables in a more centralized way.

But I could use .m-files which do not start with "function". They are still callable by filename and have access to all variables in the Workspace.

Or maybe I should substitute variables with functions? Instead of storing a value in a variable named "center", I'll call a function namned "center()" which does nothing else than return the current center value? Function calls are globally available without the need of listing all their names in every function than needs to use them.
From: Nic Roberts on
Or, load all the variable you need from the Workspace into your main program and call all function that you need from that main program. This way you can pass variables in and out of functions relatively easily without the need for globals

eg

%Main code
data1=<workspace variable1>
data2-<workspace variable2>
datapassin=[data1 data2]
xy=funXY(datapassin)
end

function modelFun=funXY(in)
.....
answers1=...
answers2=...
dataout=[answers1 answers2]

modelFun=dataout
end

BjornL <bjorn.tor.larsson(a)gmail.com> wrote in message <1953923559.41469.1279884276467.JavaMail.root(a)gallium.mathforum.org>...
> Thanks for you quick answer! Too bad one can't globalize variables in a more centralized way.
>
> But I could use .m-files which do not start with "function". They are still callable by filename and have access to all variables in the Workspace.
>
> Or maybe I should substitute variables with functions? Instead of storing a value in a variable named "center", I'll call a function namned "center()" which does nothing else than return the current center value? Function calls are globally available without the need of listing all their names in every function than needs to use them.
From: Robert on
BjornL <bjorn.tor.larsson(a)gmail.com> wrote in message <1953923559.41469.1279884276467.JavaMail.root(a)gallium.mathforum.org>...
> Thanks for you quick answer! Too bad one can't globalize variables in a more centralized way.
>
> But I could use .m-files which do not start with "function". They are still callable by filename and have access to all variables in the Workspace.
>
> Or maybe I should substitute variables with functions? Instead of storing a value in a variable named "center", I'll call a function namned "center()" which does nothing else than return the current center value? Function calls are globally available without the need of listing all their names in every function than needs to use them.

If you use scripts then all "files" that you run will see all of the workspace, but this can lead toproblems wiuth variables being changed that you dont know about. To continue using globals - why dont you put all your variables into a single global structure, i.e.

myGlobal.var1
myGlobal.var2
....

Then you only have to declare global once.

It is better to pass variables into functions as arguments rather than declaring everything as global