Reputation: 43
I am not running straight lua but the CC-Tweaks ComputerCraft version. This is an example of what I am trying to accomplish. It does not work as is.
*edited. I got a function to pass, but not one with arguments of its own.
function helloworld(arg)
print(arg)
end
function frepeat(command)
for i=1,10 do
command()
end
end
frepeat(helloworld("hello"))
Upvotes: 4
Views: 1485
Reputation: 72312
Try this code:
function helloworld(arg)
print(arg)
end
function frepeat(command,arg)
for i=1,10 do
command(arg)
end
end
frepeat(helloworld,"hello")
If you need multiple arguments, use ...
instead of arg
.
Upvotes: 2
Reputation: 72311
frepeat(helloworld("hello"))
will not pass the helloworld
function like frepeat(helloworld)
does, because it always means what it looks like: call helloworld
once, then pass that result to frepeat
.
You need to define a function doing what you want to pass that function. But an easy way to do that for a single-use function is a function expression:
frepeat( function () helloworld("hello") end )
Here the expression function () helloworld("hello") end
results in a function with no name, whose body says to pass "hello"
to helloworld
each time the function is called.
Upvotes: 3
Reputation: 394
"repeat" is reserved word in lua. Try this:
function helloworld()
print("hello world")
end
function frepeat(command)
for i=1,10 do
command()
end
end
frepeat(helloworld)
Upvotes: 1