Can you use another function as parameter?

Hello,

I was wondering if you could use a function as an argument to another function, like this…

AddCommand("Command Title", "Command Description", function()
  -- What the command does.
end)

I’ve seen it being used before, in this situation I used “AddCommand” as if you were adding commands using an already existent function called AddCommand.

2 Likes

Yes, you are able to do that. If you have a function as a parameter, that function will run first. Example:

local function Add(Number1, Number2)
	return Number1+Number2
end

local function ReturnSix()
	return 6
end

print(Add(6, ReturnSix())) --> Prints: 12
4 Likes

Ohh, I see. What if you didn’t return 6 but wanted to return actions, like Part:Destroy(), would you return the entire function?

Edit: Only works in modules, forgot. What do you return?

Returning Part:Dsetroy() would just be nil. As the normal Part:Destroy() doesn’t return anything. Are you wanting to return an action that then runs in next function call?

Well I’m trying to make a commandbar, I have everything already lined up but for future updates, I can’t be bothered to update it by adding functions. Adding commands with the function in the same line (like this)…

AddCommand("kill ".. Player.Name, "Kills the player.", function(...)
   Player.Character.Humanoid.Health = 0
end)

… Would be much easier. I’ve seen it being used before, and I’m curious as to how it works.

(AddCommand is an actual function I made, which just adds a command to my gui, informing the user that it’s an existing command)

local function Oops(...)
	local BANANA,MONKEY = ...
	print(BANANA,MONKEY) -- robux 2
	local t = {...}
	print(t[1]) -- robux
	
	return ({...})[1] -- robux
end

Oops('robux',2)
1 Like

That’s for tables with values and strings, I mean functions.

You want to put a function in a tuple? Yes, you can.
You want to use a function as your parameter? Yes.

E.g.

coroutine.wrap(function() -- the function would be our parameter
print(wait())
end)()

aaanndd

local function Oops(...)
	({...})[1]()
end

Oops(function()
	print('OMG!')
end)
3 Likes

The way that you wish to accomplish creating commands is a little backwards to me. Because when you have a function call within the function call, it’s going to run when you do the AddCommand. You should have a table of the commands then call the function from the table.

local Commands = {
	Kill = (function(Player)
		Player.Character.Humanoid.Health = 0
	end)
}

wait(1)
Commands.Kill(game.Players.EXAMPLEPLAYER)
1 Like

Functions are first-class types in Lua, meaning variables can hold functions as values, functions can be passed to other functions as arguments, and functions can be returned from functions.

E.g.

local sum = 0
table.foreach(list, function(key, value) sum += value end)

or

function curry(func, arg)
    return function(...)
        return func(arg, ...)
    end
end

local customPrint = curry(print, "My Print:")
customPrint("test") -- -> My Print  test
5 Likes