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!

#### Top Posters

• ar2rsawseen 6991
• SinisterSoft 3878
• hgy29 2649
• keszegh 2290
• antix 2072
• OZApps 1983
• totebo 1764
• oleg 1630
• hgvyas123 1412
• techdojo 1321
• phongtt 1029
• Mells 1024
• MikeHart 1020
• john26 995
• GregBUG 962
• pie 962
• MoKaLux 948
• talis 916
• Apollo14 901
• Scouser 898

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

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:

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