Am I correct?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Krizzu
Prole
Posts: 20
Joined: Sun Apr 15, 2012 8:02 pm

Am I correct?

Post 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!"
User avatar
Inny
Party member
Posts: 652
Joined: Fri Jan 30, 2009 3:41 am
Location: New York

Re: Am I correct?

Post 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.
coffee
Party member
Posts: 1206
Joined: Wed Nov 02, 2011 9:07 pm

Re: Am I correct?

Post 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
Last edited by coffee on Sun Sep 09, 2012 9:02 am, edited 1 time in total.
User avatar
qaisjp
Party member
Posts: 490
Joined: Tue Sep 04, 2012 10:49 am
Location: United Kingdom
Contact:

Re: Am I correct?

Post by qaisjp »

Lua is not an acronym.
User avatar
tv_user
Citizen
Posts: 57
Joined: Sun Aug 12, 2012 4:39 pm
Location: Portugal

Re: Am I correct?

Post 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 :ultrahappy:
Did my comment help/offended you? Use the Karma button!!
LÖVEing each day...
Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests