Page 1 of 1
Am I correct?
Posted: Sun Sep 09, 2012 2:11 am
by Krizzu
Code: Select all
local mySecretVariable = "A secret!"
local mySecretVariable2 = "Another secret."
function getTheSecret( firstSecret )
if ( firstSecret ) then
return mySecretVariable
else
return mySecretVariable2
end
end
print( getTheSecret() )
What should it print?
I think it would print "Another secret" because argument "firstSecret" is not given when the function is call out.
But in page where i found it it says it would print "A secret!"
Re: Am I correct?
Posted: Sun Sep 09, 2012 2:18 am
by Inny
You're correct, without any parameters given to the function call, firstSecret is set to nil, which in an if condition lua treats as a logical false.
Re: Am I correct?
Posted: Sun Sep 09, 2012 5:29 am
by coffee
If you run that instead in Lua, Love or an online compiler you wouldn't have to ask if u correct! And is not secure ask to us. We could always error or lie to you (as in that that webpage did) but a Lua interpreter won't. ;D
Re: Am I correct?
Posted: Sun Sep 09, 2012 8:47 am
by qaisjp
Re: Am I correct?
Posted: Mon Sep 10, 2012 1:50 pm
by tv_user
I've already made this mistake sometimes, so just to give you a warning:
Unlike many other programming languages (for example C++), Lua doesn't assume 0 to be
false. There are only two values that Lua treats as a logical
false, namely: false and nil. Every other value (even zero) is assumed to be
true.
Happy coding