Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function giving strange error?

Tags:

function

lua

    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!

like image 231
Ethan Hammond Avatar asked Jun 07 '26 17:06

Ethan Hammond


1 Answers

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))
like image 176
Ankur Avatar answered Jun 10 '26 19:06

Ankur