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

- 7.8K All Categories
- 11 Help with translations
- 4.2K General questions
- 106 Roadmap
- 358 Game & application design
- 315 Plugins
- 70 User experience
- 75 Marketplace
- 290 Code snippets
- 34 Building a team?
- 269 Suggestions & requests
- 352 Announce your apps made with Gideros.
- 90 Step by step tutorials
- 666 Bugs and issues
- 195 Introduce yourself
- 238 Announcements
- 106 Forum talk
- 403 Relax cafe

zamond
Member

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

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

Tagged:

## Comments

are you using gideros?nil because x is defined locally in the function squareAndCube (x), it is only visible in this function.

my growING GIDEROS github repositories:https://github.com/mokalux?tab=repositoriesfunction 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

codeI am happy to help

Likes: zamond

my growING GIDEROS github repositories:https://github.com/mokalux?tab=repositoriesLikes: MoKaLux

Likes: zamond, SinisterSoft, MoKaLux