Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Why is x returning nil? The return statement works for other vars, but not x — Gideros Forum

Why is x returning nil? The return statement works for other vars, but not x

zamondzamond Member
edited September 2020 in Code snippets
Hi, Lua noob here. Just started today and have this question about functions and return statements. In the following snippet, the value of x is nil, and I cannot understand why. Can someone help?

function squareAndCube (x)
print ("Enter any number to get the square and cube:")
x = io.read()
square = x * x
cube = square * x
print ("The number you entered, its square, and cube are:")
return x, square, cube
end
print (squareAndCube ())
print (x)
print (square)
print (cube)

--------------------- (OUTPUT)------------------

Enter any number to get the square and cube:
5
The number you entered, its square, and cube are:
5 25 125
nil
25
125

Comments

  • MoKaLuxMoKaLux Member
    edited September 2020 Accepted Answer
    are you using gideros?

    nil because x is defined locally in the function squareAndCube (x), it is only visible in this function.
    function squareAndCube (n) -- i changed the variable name to n
    	print ("Enter any number to get the square and cube:")
    	--x = io.read() -- this has no effect in gideros
    	x = n -- here x will be visible in the whole code (global variable)
    	square = n * n -- (global variable)
    	cube = square * n -- (global variable)
    	print ("The number you entered, its square, and cube are:")
    	return x, square, cube
    end
     
    print (squareAndCube (3)) -- example with 3
    print (x) -- (global variable)
    print (square) -- (global variable)
    print (cube) -- (global variable)
    :)
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
  • Thank you! I was out walking and pondering this, and I realized it must be something like that. This is what I came up with:

    function squareAndCube (x)
    print ("Enter any number to get the square and cube:")
    x = io.read()
    square = x * x
    cube = square * x
    xinput = x
    print ("The number you entered, its square, and cube are:")
    return xinput, square, cube
    end
    print (squareAndCube ())
    print (xinput)
    print (square)
    print (cube)

    -----------------------------

    I was also able to solve it just be removing x from the function argument:

    function squareAndCube ()
    print ("Enter any number to get the square and cube:")
    x = io.read()
    square = x * x
    cube = square * x
    print ("The number you entered, its square, and cube are:")
    return x, square, cube
    end
    print (squareAndCube ())
    print (x)
    print (square)
    print (cube)

    Thanks again for your help!

    ---------------------------------

    On an unrelated note, how are you getting your code snippets to appear formatted they way you are?

    Likes: MoKaLux

    +1 -1 (+1 / -0 )Share on Facebook
  • MoKaLuxMoKaLux Member
    edited September 2020 Accepted Answer
    zamond said:

    On an unrelated note, how are you getting your code snippets to appear formatted they way you are?

    you highlight your code and you pick in the toolbar code

    I am happy to help :)

    Likes: zamond

    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
    +1 -1 (+1 / -0 )Share on Facebook
  • function squareAndCube ()
        print ("Enter any number to get the square and cube:")
        x = io.read()
        square = x * x
        cube = square * x
        print ("The number you entered, its square, and cube are:")
        return x, square, cube
    end
    print (squareAndCube ())
    print (x)
    print (square)
    print (cube)
  • awesome, thank you!!!

    Likes: MoKaLux

    +1 -1 (+1 / -0 )Share on Facebook
  • hgy29hgy29 Maintainer
    Accepted Answer
    Although this is perfectly valid code and I am convinced that a programmer can totally write "bad code" if it turns out to be more efficient or the best way to achieve a particular goal, I think you should have written it this way instead:
    function squareAndCube ()
    	print ("Enter any number to get the square and cube:")
    	local x = io.read()
    	local square = x * x
    	local cube = square * x
    	print ("The number you entered, its square, and cube are:")
    	return x, square, cube
    end
    local x,square,cube=squareAndCube ()
    print (x,square,cube)
    print (x)
    print (square)
    print (cube)
    Main reason is: avoid polluting your global variable name space
    +1 -1 (+3 / -0 )Share on Facebook
  • Haven't gotten that far yet :)
Sign In or Register to comment.