Ethan Hammond
Ethan Hammond

Reputation: 31

Function giving strange error?

    function returnNumPlus1(num)
    return num + 1
end

print(returnNumPlus1(0))

print(returnNumPlus1(9000))

local func1 = returnNumPlus1
print(func1(11))

I was testing it to try to get it to work without error, but i always get the same error as i post below. Im fairly new to lua, so i hope i can get this to work :D and gives off the error:

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

does anyone know why? Thanks!

Upvotes: 3

Views: 48

Answers (1)

Ankur
Ankur

Reputation: 33657

Assuming you are running this code in lua REPL, you need to define func1 as global rather than local as 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))

Upvotes: 3

Related Questions