Does the function give a strange error?

    function returnNumPlus1(num)
    return num + 1
end

print(returnNumPlus1(0))

print(returnNumPlus1(9000))

local func1 = returnNumPlus1
print(func1(11))

      

I've tested it to try and get it to work without error, but I always get the same error as the message below. I'm new to lua, so I hope I can get this to work: D and throws an error:

stdin:1: attempt to call global 'func1' (a nil value)
stack traceback
        stdin:1: in main chunk
        [C]: ?

      

Does anyone know why? Thank!

+3


source to share


1 answer


Assuming you are running this code in the lua REPL, you need to define it func1

as global and not local, since the local context is specific to each line execution in the REPL and is not available for the next line.

Try:



function returnNumPlus1(num)
    return num + 1
end

print(returnNumPlus1(0))

print(returnNumPlus1(9000))

func1 = returnNumPlus1
print(func1(11))

      

+3


source







All Articles