How to properly use Passed Parameters in a Function?

Yes, it’s time for another late night session of “what glaringly obvious newbie mistake did I make this time?”

[code]local function printRank(type)
if type == Football then
print ( “Football” )
elseif type == Basketball then
print ( “Basketball” )
elseif type == Hockey then
print ( “Hockey” )
end
end

printRank(Hockey)[/code]

The above prints “Football”, no matter what is passed through the function (eg: printRank(turkeysandwich)). If I try to set type to a variable, it just comes up as nil.

Must be missing something really obvious, but how do you pass parameters to a function and have it act on them differently based on what you use?

Cheers :slight_smile:

[import]uid: 41884 topic_id: 12846 reply_id: 312846[/import]

Ho.

I do not see anything obvious. The only thing is the word “type”. Could it be a keyword since it is colored blue in your code? Try another word/word like t or temp.

Good luck.

Mo [import]uid: 49236 topic_id: 12846 reply_id: 47117[/import]

Hi

Here is how to pass parameters properly
[lua]local function printRank(params)
if params.Type == “Football” then
print ( “Football” )
elseif params.Type == “Basketball” then
print ( “Basketball” )
elseif params.Type == “Hockey” then
print ( “Hockey” )
end
end

printRank({ Type = “Hockey”})[/lua] [import]uid: 46546 topic_id: 12846 reply_id: 47124[/import]

I know you are testing the function, but an easier way to just test sending the parameters would be

[lua]local function printRank(params)
print(params.Type)
end

printRank({Type=“Hockey”})[/lua]

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 12846 reply_id: 47139[/import]

mko2111 : After slamming my head against your example and failing to get results…I see it’s the quotes.

if params.type = “CBS” works but = CBS does not. -_- Which seems strange because variable calculations usually don’t need that at all. (eg: if i == 37 )

What I don’t understand is why it has to be { Type = “ESPN” } inside the brackets? How do I make it a conventional function that just does function(x)?

ie: display.newRect()

jayantv : Yeah, I figured :slight_smile: However I’m not actually using the print commands for anything but figuring out why the function isn’t working. I actually need the three inputs so I can perform one of three different table.sort commands…)
[import]uid: 41884 topic_id: 12846 reply_id: 47241[/import]

You need to put it in quotes to make it a string, otherwise you’re referring to a variable that does not exist. You don’t need quotes on numerical values.

Example:

[code]
local myText = “Hello”;

local function printA( theMessage )
print( theMessage ); – [THIS PRINTS: Hello]
end

printA( myText );

local function printB( theMessage )
print( theMessage ); – [THIS PRINTS: myText]
end

printB ( “myText” );
[/code] [import]uid: 49978 topic_id: 12846 reply_id: 47246[/import]

…That makes perfect sense. My brain struggles to not confuse strings with variables when it comes to the (params) problem, but I still understand what you’re getting at.

So it sounds like (based on my pre-reply struggles):

a. params.Type fails if you forget to capitalize the “T”
b. To answer my own question, I should use what warpser suggests. Instead of ({ Type = something}), this works instead:

[code]local function printRank(ranktype)
if ranktype == “Size” then
print (“Size wins!”)
elseif ranktype == “Noise” then
print (“Noise wins!”)
elseif ranktype == “Landspeed” then
print (“Landspeed wins!”)

printRank(“Noise”)[/code]

Or to paraphase what warpser is saying, printRank(Noise) wouldn’t work because it’s not a string, but printRank(“Noise”) would. Going without the quotes would mean pre-setting “Noise” into a variable.

Thanks everyone :slight_smile:
[import]uid: 41884 topic_id: 12846 reply_id: 47252[/import]

@richard9,
mate

local age=14
local name=“Richard”

Do you know the difference between the two types of variables? One is a number and one is a string. numbers do not require the quotes " where as the strings are enclosed in quoted.

so when you are comparing the values, you would use

[lua]if age < 18 then
print (“Under 18”)
end

if name == “Richand” then
print(“Hello Richard”)
end[/lua]

you can also have
[lua] local myName = 1
if myName == “Jayant” then
print(“This one should not evaluate”)
end[/lua]

hope that helps you understand the types of variables and how to compare them.

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 12846 reply_id: 47264[/import]

>>>>
What I don’t understand is why it has to be { Type = “ESPN” } inside the brackets?
How do I make it a conventional function that just does function(x)?

ie: display.newRect()
>>>>

the data passed in {} is treated as a table.
so if you were to do this
[lua]local myTable={name=“Richard”,topic=“Tables”,keyword=“function”}

local function doCalled(param)
print("This function is called by " … param.name)
print("The topic of dicussion is " … param.topic)
print("The keyword in question is " … param.keyword)
end

doCalled(myTable)
local doCalled1(name,topic,keyword)
print("This function is called by " … name)
print(“The topic of dicussion is " … topic)
print(” The keyword in question is " … keyword)
end

doCalled1(“Jayant”,“Help”,“FAQ”)[/lua]
hope that helps,

?:slight_smile: [import]uid: 3826 topic_id: 12846 reply_id: 47265[/import]

*mind blown*

That’s perfect, jayan. I’ll definitely be using that. I figured out the string/var difference thanks to warpser. :slight_smile:

Big thanks to all readers and repliers; my programming language knowledge is limited to slapdash, inconsistent stuff so this has been a heck of a learning curve.

Now I just gotta figure out if I can reference external tables instead of merely functions(although I get the impression maybe SQLite should be used…I’m really not clear on when to draw the line between table-arrays or SQL? Is 300 entries x 10 categories too much for tables?)

Nonetheless, thanks everyone :slight_smile: [import]uid: 41884 topic_id: 12846 reply_id: 47408[/import]