Can a function become a parameter in a function?

Can a function become a parameter in a function. Can’t test right now, on the way home, using my mobile device posting this sorry

but here is the code that runs on my mind

lua1.lua

outerFunction = function()

end

0-----0

require lua1

lua2.lua

func = function(outerFunction)

end

That shouldn’t be a problem.

You can pass a function to another function. That’s basically how delegates work.

Before you call a function that has been passed as an argument you can check if it’s a function by doing something like:

local func = function(outerFunc) if type(outerFunc) == "function" then outerFunc() end end

As <ingemar> says, yes, this is possible. This is how callbacks work. See this post by Rob Miracle about how to do this with modules: http://coronalabs.com/blog/2014/07/01/tutorial-handling-callback-functions-in-modules/

I’ve done this and it works perfectly fine

but what If I have parameters outside the outerFunction?

There are multiple ways of doing it.

Maybe the simplest would be to add them to the calling function.

Something like:

local myFunc = function(outerFunc, p1, p2, p3) if type(outerFunc) == "function" then outerFunc(p1, p2, p3) end end

Oh alright thanks ingemar :slight_smile:

Functions in Lua are “first class citizens” which means they can be passed by reference just like normal variables. It also means that, like variables, you can put them into tables and call them dynamically - something typically called “reflection” in many other languages. For example:

local function a(b) print("a: ",b) end local t = {} t.a = a local func = t["a"] func("param")

This code will print out:

param

That shouldn’t be a problem.

You can pass a function to another function. That’s basically how delegates work.

Before you call a function that has been passed as an argument you can check if it’s a function by doing something like:

local func = function(outerFunc) if type(outerFunc) == "function" then outerFunc() end end

As <ingemar> says, yes, this is possible. This is how callbacks work. See this post by Rob Miracle about how to do this with modules: http://coronalabs.com/blog/2014/07/01/tutorial-handling-callback-functions-in-modules/

I’ve done this and it works perfectly fine

but what If I have parameters outside the outerFunction?

There are multiple ways of doing it.

Maybe the simplest would be to add them to the calling function.

Something like:

local myFunc = function(outerFunc, p1, p2, p3) if type(outerFunc) == "function" then outerFunc(p1, p2, p3) end end

Oh alright thanks ingemar :slight_smile:

Functions in Lua are “first class citizens” which means they can be passed by reference just like normal variables. It also means that, like variables, you can put them into tables and call them dynamically - something typically called “reflection” in many other languages. For example:

local function a(b) print("a: ",b) end local t = {} t.a = a local func = t["a"] func("param")

This code will print out:

param