Any way to change Local Vars values inside a Function?

Discuss any general programming issues here
Post Reply
Bugala
Posts: 1259
Joined: Sun Feb 14, 2010 7:11 pm

Any way to change Local Vars values inside a Function?

Post by Bugala »

I just noticed I am constantly writing same lines of code, and I am now starting to wonder if there would be a way to change this into a Function.

Lines go like this:

Code: Select all

Function Func(Var, OptionalArgs)
Local Varname = "default"
Local Varname2 = "default"
if OptionalArgs <> Nil
   If Hasitem(OptionalArgs, "varname") then Varname = OptionalArgs.Varname
   If HasItem(OptionalArgs, "varname2") Then Varname2 = OptionalArgs.Varname2
...
Endif
Real example could be that there is for example variable called "Duration".

At beginning of Function I am making Duration a Local variable, and assigning it some default value, but, if OptionalArgs has Duration in it, then this default value is replaced with the one in the OptionalArgs.

example usage:

Code: Select all

Func(12, {duration=100})
What I notice is that I keep writing these same lines of code all the time, although there are some differences in what OptionalArgs I am checking each time.

But all in all, I could well have just a Function that would check through all these OptionalArgs, even if it would then check through some unnecessary arguments, since they anyway wouldn't affect anything.

Idea being:

Code: Select all

Function Func(Var, OptionalArgs)
HandleOptionalArguments(OptionalArgs)
I could do something like:

Code: Select all

Local O = {}
HandleOptionalArguments(O, OptionalArgs)
Debugprint(O.Varname1)
But I would prefer to have them just as local Varname, instead of O.Varname.

But is there any way to do this in such way that when I jump to "HandleOptionalArguments" function, that it would be able to assign values to Local variables in the function from where it jumped to that function?

There is for example this, was it "G[VarName]" which I can use to alter Global variables, but is there any way to alter Local functions same way perhaps?
Post Reply