Hello, I don’t understand that does ‘:’ in a function?
like in this line :
local function changeValue(name: string, amount: number)
the comma I understand but what do the colons do? it’s like a = ?
Hello, I don’t understand that does ‘:’ in a function?
like in this line :
local function changeValue(name: string, amount: number)
the comma I understand but what do the colons do? it’s like a = ?
It basically has the function assume what the variable is going be, or at least what you expect the value to be when using it.
So if you have a function that is intended to calulate a value, you can assign the parameter as a number, another function, or whatever you want it to be. It will have no affect on how it works, it just makes it assume that its something else.
You can do the same thing with a normal variable as well.
local var: number = nil -- assumes that var is a number
-- when its real value is nil
So you could think of it that way, its kind of like saying
Name = a string value
or
Amount = a number value
You can also do this with what it will return, which would look like this:
function changeValue(name: string, amount: number): number
Which as you guessed, assumes that it will return a number value.
Keep in mind that it will not determine what the value will be, it only forces the parameter to say that it’s a
number
orstring
, but in reality it could be a very different thing. I could put aVector3
value in there and it could end up affecting the function.