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

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

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

Member
edited September 24
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:")
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
Tagged:

• Member
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
• Member
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:")
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:")
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)

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

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
• Member
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
• Member
 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)
• Member
awesome, thank you!!!

Likes: MoKaLux

+1 -1 (+1 / -0 ) Share on Facebook
• Maintainer