is there any way for Function to hold variables?

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

is there any way for Function to hold variables?

Post by Bugala »

I know I can do this:

Code: Select all

myvar = {}
myvar.var = 10

function myvar.func()
debugprint(myvar.var)
endfunction
But just from cleaner code point of view, I would rather have something like:

Code: Select all

Function Func()
func.var=10
debugprint(func.var)
Endfunction
Is something like this doable any way?
Flinx
Posts: 188
Joined: Sun Feb 14, 2021 9:54 am
Location: Germany

Re: is there any way for Function to hold variables?

Post by Flinx »

Hi Bugala,

I know that you ask Andreas, but I'm trying to understand what you're hoping to achieve.
In your first snippet you have the global table myvar containing the variable var and the function func. func prints the variable var from the table.

But what should be func.var in the second snippet? Something like a local variable of the function Func()? In Hollywoods syntax I think this would be an element of a table func, not of a function. And what are the connections to the first code snippet?
I the subject you write "hold variables". Do you mean local variables that hold their value over several function calls but are not visible from outside the function, as is done in C with the static keyword?

Ralf
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

Re: is there any way for Function to hold variables?

Post by Bugala »

Lets put it like this.

What I am after is basically that if I decide to either reuse the code, or for example delete that function, I would in that case just want to delete stuff between FUNCTION and ENDFUNCTION.
I don't want to need to check if that code continues beyond those two, like in the first examples case I would need to.

As in:

Code: Select all

var = {}
var.x = 1

FUNCTION var.func()
ENDFUNCTION
If I decide I don't need this Function anymore, I would then need to notice that I also need to delete those two lines before to really delete that function completely.


Real case that just came to me in my code is that I am going to be having a function, which needs to hold sort of timer variable.

Code: Select all

FUNCTION animation()
Timer = Timer + Timepassed

If Timer = 1 then...
If Timer = 2 then...
If Timer > 3 then Timer = 0

ENDFUNCTION

Right now I either need to make that timer variable outside of that function, or to use the

Code: Select all

myvar = {}
myvar.timer = 0

FUNCTION myvar.func()
myvar.timer = myvar.timer + timepassed
ENDFUNCTION


So I would prefer having a system where that whole thing would be contained between FUNCTION and ENDFUNCTION, instead of it spreading outside of it.

Also, since if I don't use myvar option, then that timer would need to be global and could accidentally collide with something else at later point.
Flinx
Posts: 188
Joined: Sun Feb 14, 2021 9:54 am
Location: Germany

Re: is there any way for Function to hold variables?

Post by Flinx »

Thank you, I see. It seems like you want what I mentioned in my last sentence.

Code: Select all

Static Timer = Timer + Timepassed
User avatar
jPV
Posts: 603
Joined: Sat Mar 26, 2016 10:44 am
Location: RNO
Contact:

Re: is there any way for Function to hold variables?

Post by jPV »

Lua doesn't have static variables, so I guess it's out of question for Hollywood too and you'll have to use global variables.

Maybe you should just decide a pattern to name these kinds of variables so that you don't use them elsewhere accidently. For example a global variable used in the animation function could be something like g_func_animation_timer and so on.
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

Re: is there any way for Function to hold variables?

Post by Bugala »

Yeah, I been using G_var naming convention in these kind of cases, but especially in this case when this G_Var is used only in this single function, it is somewhat inconvenient to resort to global function from coding style point of view.
User avatar
emeck
Posts: 169
Joined: Fri Apr 03, 2015 3:17 pm

Re: is there any way for Function to hold variables?

Post by emeck »

What about assigning the function to a variable? Something like this maybe (haven't tested the code, just an idea):

Code: Select all

p_myvar = Function(...) Local pars = arg.n Return({pars[0],pars[...],pars[n]}) EndFunction

result1 = p_myvar(5, 2)
result2 = p_myvar(8)
result3 = p_myvar(5, 2, 4)
You can also write functions to with fixed values only or use fixed values in case no parameters are given.

Edit:
Maybe this is better for different number of arguments:

Code: Select all

p_myvar = Function(...) Return(Unpack(arg)) EndFunction
PowerBook 5.2 MorphOS 3.15
PowerBook 5.8 MorphOS 3.15
Amiga 1200 BPPC/BVision AOS4.1 FE
Flinx
Posts: 188
Joined: Sun Feb 14, 2021 9:54 am
Location: Germany

Re: is there any way for Function to hold variables?

Post by Flinx »

jPV wrote: Fri Nov 18, 2022 8:42 pm Lua doesn't have static variables, so I guess it's out of question for Hollywood too
Of course I don't know much about the way Hollywood generates Lua code, but that shouldn't be a problem. Such a "Local Static varname" only needed to be translated into a global variable that is only used in this function. For me the question is more whether this would be an improvement for Hollywood.
Flinx
Posts: 188
Joined: Sun Feb 14, 2021 9:54 am
Location: Germany

Re: is there any way for Function to hold variables?

Post by Flinx »

Hi Bugala,

If it's just a question of putting everything into the function, then perhaps the following structure might help:

Code: Select all

Function myfunc()
	If IsNil(myvar) Then myvar=5
	myvar=myvar+1
	DebugPrint(myvar)
EndFunction

For i=1 To 3
	myfunc()
Next
You then just have to use a safe name for myvar that can't collide with the rest of the program.
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

Re: is there any way for Function to hold variables?

Post by Bugala »

@emeck, that could work, going to consider this one, although basically in this case I guess I will revert to @Flink idea.

Thanks @Flink, that seems like a very easy solution to this problem, although it still does leave that variable Global, which @Emecks solution would avoid.
Post Reply