# Better Explanation of Function Parameters

yes nigga’s I post a lot.

anyways

So guys I have been having a lot of trouble creating custom code in GLua. Mainly because I just don’t get function parameters.

Let me give you guys an example:

``````

-- add all elements of array `a'
local sum = 0
for i,v in ipairs(a) do
sum = sum + v
end
return sum
end

``````

– Source: http://www.lua.org/pil/5.html

So, I just don’t understand what the “a” is in the parenthesis is… I mean can it be anything? Like can:

``````

function ass (ply)
print(ply.. "is gay.")
end

``````

Be:

``````

function ass (myfingeredbutthole)
print(myfingeredbutthole.. "is gay.")
end

``````

And if so, HOW?! I mean I’m just so confused. Can someone help me?

It’s the parameter name, it can be anything as long as it doesn’t conflict with the language keyword names such as end, do, then.

I’m not sure how much more I could explain but uh…

say you had code like this

``````

local lol = true

``````

well the variable “lol” is now assigned to true in the local scope

and so if we had a function like this

``````

function IsItTrue(some_variable_name)

print(some_variable_name == true)

end

``````

and we used it like this

``````

IsItTrue(lol)

``````

the true that “lol” is assigned to is passed into the function and onto the parameter variable. And so it would print “true” into console because the value that’s assigned to “some_variable_name” is true and true is equal to true

EDIT: The parameter is a local variable to the function, probably a better way to word it.

I’m so confused… Can you use an example in GLua?

[editline]2nd March 2016[/editline]

Or even the following:

``````
function f(a, b) return a or b end

``````

– Source: http://www.lua.org/pil/5.html

What does this return?

[QUOTE=Percipience;49855490

``````
function f(a, b) return a or b end

``````

[/QUOTE]

``````

function f(a, b) return a or b end

print (f ("hello"))

``````

hello

``````

function f(a, b) return a or b end

print (f (nil,"doe"))

``````

> doe (im not actually testing this cause u hurt my feelings)

What I put is “GLua”, Lua and GLua are the same just a few language differences

And I edited my post but I’ll say it again, think of function parameter as variables that are local to the function, as that’s what they are.

An example of the function you gave would return different things depending on what you gave it…

``````

f(1, 2)

``````

Would return 1 because 1 is valid

While if you did

``````

f(nil, 2)

``````

Would return 2 as nil is not valid and so it goes to 2

To give examples as to what those are in the first example the function is pretty much doing this

``````

return 1 or 2

``````

While in the second example the function is doing this

``````

return nil or 2

``````

The parameters in your function represent whatever the function is being called with. Like this:

``````
function PrintIsTwo(a)
if a == 2 then
print("Yep, that's two!")
else
print("Nope, that's not two!"
end
end

PrintIsTwo(1)
PrintIsTwo(2)
PrintIsTwo(3)

``````

This prints “Nope, that’s not two!”, then “Yep, that’s two!”, then “Nope, that’s not two!” again.

okay, I think I have a general understanding. however, take the “Demote Upon Death” on the DarkRP Wiki for example:

``````

PlayerDeath = function(ply, weapon, killer)
ply:teamBan()
ply:changeTeam(GAMEMODE.DefaultTeam, true)
if killer:IsPlayer() then
DarkRP.notifyAll(0, 4, "The mayor has been killed and is therefor demoted.")
else
DarkRP.notifyAll(0, 4, "The mayor has died and is therefor demoted.")
end
end

``````

The function isn’t being called? Like how does it know “ply:teamBan()” is even a player?

or

ply:changeTeam(GAMEMODE.DefaultTeam, true)

??

[editline]2nd March 2016[/editline]

also, how does it know “killer:IsPlayer()”

is even the killer of that person? I mean aren’t these parameters like localized variables?

[editline]2nd March 2016[/editline]

Anyone?

Somewhere else in the code that function would be ran. The gamemode probably calls it.
PlayerDeath(ply,weapon,killer)
the parameters will be replaced with the proper values.

ex. PlayerDeath(Player1,“smg1”,MegaMech)

in ply:TeamBan() if ply is not a player it will throw an error because ply might not be nil, but ply.TeamBan() would be nil. (Trying to index nil value)

killer is a table value (I think) as well as ply. IsPlayer() probably checks the table to see if a certain key or boolean is true. Might be able to look up the source code perhaps?

Parameters work exactly as local variables, initialized with the actual arguments given in the function call. You can call a function with a number of arguments different from its number of parameters. Lua adjusts the number of arguments to the number of parameters, as it does in a multiple assignment: Extra arguments are thrown away; extra parameters get nil. For instance, if we have a function like

``````

function f(a, b) return a or b end

``````

I got this from:

So basically if you make a function parameter with the name “ply” you wouldn’t have to call it in Garry’s Mod? Since it’s already called somewhere else?

ULX is my best example here
[lua]
function ulx.stuck(calling_ply)

ULib.tsay(calling_ply, “SAY SOMETHING I’M GIVING UP ON YOU”)

end
local stuck = ulx.command( CATEGORY_NAME, “ulx stuck”, ulx.stuck, “/stuck” )
stuck:help( “When you are stuck this command will respawn you”)
[/lua]
When someone say’s /stuck in chat. ulx runs command ulx.stuck and adds the parameter calling_ply.
The game knows who typed /stuck in chat. So it passes this on to ulx.stuck(calling_ply)
It works the sameway for any function with (ply) in it. I can change calling_ply to anything. It doesn’t matter because all the game does it pass the Entity() into the parameter. the variable could be called “thisIsDefinitelyNotAnEntity” and it would still work, but anyone trying to read my code would hate me for it.
I could also do ply = LocalPlayer() or plyName = LocalPlayer():GetName()
(Note that if all entites are not initialized LocalPlayer will be nil)
Comprendo?

Nope

How does it pass it to “calling_ply” I mean, there has to be something calling this function. Right?

Yes, there is back end code calling ulx.stuck. Something along the lines of this: (Except it would be waaay more complex)
[lua]
hook.run(“OnChat”, function(calling_ply, msg)
if msg == “/stuck” then do ulx.stuck(calling_ply) end end
end)
[/lua]
Now you’ll be wondering where msg comes from. I have no idea… It’s just there, God did it. I dunno. More back end code that is not displayed here.
This is why the Gmod API is soo important. Fretta for example has tons of this. A gamemode that uses Fretta has tons of predetermined function names and arguements that Fretta calls. You won’t know how a function get’s called unless you’re looking at a document that specifically tells you when it is going to run.

I see, thank you mate!