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

- 7.4K All Categories
- 8 Help with translations
- 4.1K General questions
- 99 Roadmap
- 331 Game & application design
- 294 Plugins
- 66 User experience
- 70 Marketplace
- 280 Code snippets
- 33 Building a team?
- 253 Suggestions & requests
- 346 Announce your apps made with Gideros.
- 88 Step by step tutorials
- 611 Bugs and issues
- 187 Introduce yourself
- 194 Announcements
- 85 Forum talk
- 389 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