Is it possible to send Function as argument?

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

Is it possible to send Function as argument?

Post by Bugala » Fri Feb 12, 2016 9:26 am

Normally if i use:

Code: Select all

generalfunction(a, my_func)
What will be sent to generalfunction as arguments would be variable a:s content, and result of "my_func".

However, right now i have a situation where it would be handy if i could actually send the function as it is, to be executed at later point, but is this possible?

What I am after with this in practice is, that I am about to create some buttons, which work exactly same, except, when they are being clicked, they are supposed to execute some function, like "StartGame()".

Therefore it would be handy if i could make only one button type, where i could send that function to be executed when that button is being clicked:

(partial code)

Code: Select all

p_Create_Button(FuncToUse)
evttable = {OnMouseUp = FuncToUse}
Is this possible to do somehow? Could i send a reference to a function for example somehow?

User avatar
airsoftsoftwair
Posts: 2832
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Is it possible to send Function as argument?

Post by airsoftsoftwair » Fri Feb 12, 2016 4:18 pm

Bugala wrote:Normally if i use:

Code: Select all

generalfunction(a, my_func)
What will be sent to generalfunction as arguments would be variable a:s content, and result of "my_func".
No, this code won't pass the result of "my_func" to generalfunction() but it will pass the function "my_func" to generalfunction(), i.e. it will do exactly what you asked for. For more details see here:

http://www.hollywood-mal.com/docs/html/ ... cVar_.html

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

Re: Is it possible to send Function as argument?

Post by Bugala » Fri Feb 12, 2016 11:29 pm

Ah. Finally got it. Had been wondering why on those "OnMouseOver" and ForEach(table, dofunc) it have been demanding function to be without the ()-part.

So idea is that "MyFunc()" results in giving result of the function, while "MyFunc" is sending the function itself.

This is very helpful.

Post Reply