What are parameters ffor?

I don’t understand how parameters and how they work, how does it help if you don’t set it to a string, enum or integer?

You can use parameters like this:

local function printSomething(thingToPrint)
print(thingToPrint)
end

printSomething("hello world")

I recommend watching AlvinBlox’s tutorial about this topic.

1 Like

They dynamically change on how you want to call that function. They are like adverbs to put it that way.

https://www.lua.org/pil/5.2.html
All you ever need to know about lua is specified above.

Well parameters are used in functions, so what you should really be trying to do is understand how functions utilize parameters.

When we force values, we do something called hard coding. This essentially means that we can only edit the code through the source code and then compiling it instead of getting values at run time.

So how do we use parameters in our function?

Imagine a function like a machine, here is a quick one:

function pythagoras(a, b) -- our parameters are 'a' and 'b'
	return ((a^2)+(b^2))^0.5 -- 'a' squared + 'b' squared = 'c' squared >> pythagoras theorem
end

local a = pythagoras(3, 4)
local b = pythagoras(4, 5)
print(a, b)

In this scenario, we want to produce something in our function which might be useful or cause an operation based on the information. Now we don’t want to write a dedicated function like so:

function pythagoras_of_3_and_4()
	return 5
end

print(pythagoras_of_3_and_4())

To handle each scenario like so we’ll end up having 10000 lines of just pythagoras just to handle the most likely scenarios - it has limitations which parameters can defeat.

By having parameters, we are saying ‘if value x is inserted into the function, do … with x’ instead of having fixed values for our functions - this makes our programming more dynamic and allows us to do the same operation on different values without the concerns.

So what about limitations when we don’t set things?

Well lets say we had the function:

function multiply(a, b)
	return a * b
end

multiply(4, 7) -- this will work
multiply("cracker", "cheese") -- this will not work

Sometimes, especially with user inputs, we have to be prepared to handle parameters which will break functions which is where tonumber() can come in use - in most circumstances though you can verify and assure yourself that on server side code you won’t have to handle freak circumstances especially when you know the value will always be a number - so as such seeming as before I said we want to be able to apply parameters dynamically we may have to add proof checks to make sure our dynamic functions don’t automatically break.

But you don’t always need to use functions for their parameters…

A useless function might be a multiply by five function, you don’t need to use the multiply function above if you know b is always going to be five - it can be a lot easier to just do a * 5 outside of the function. This is especially done when you have strict values, if you know what a value is always going to be dependent on and its only used singularly then feel free to hard code.

Functions really (in my eyes at least) are used mainly for four things:

  • when you want to perform the same operation in two sets of code - this stops repeat code.
  • when you want to perform operations where the product is dependent on more then a single variable.
  • when you need to give order to your code in some scenarios (for example if a player owns a sword you may want to weld the sword before you weld the avatar - if they don’t just weld the avatar → you can then call the functions in a specific order).
  • when you want to make long code look neater out of preference - as such your using your functions to organise operations into parts.

Hope this gives you some basic insight :slightly_smiling_face:

2 Likes