What is the correct way to write a function?

Over the past couple years I’ve overlooked this question and never really though about it. There are two different ways to write a function and they both seemingly do the same thing and I’ve been using the two interchangeably without realizing it. Here’s what I mean:

--Which is better?
local function name()
end

local name = function()
end

The two samples above are both valid functions, but what’s the difference?
Is one of them the “correct” way? Is one more efficient than the other? It seems like such a silly question to ask considering that functions are fundamental to scripting but it’s something that I’ve neglected to ask for a while.

1 Like

With your first function, it won’t run until provided a use, and can be used multiple times for different variables. This means if you have a function which is shootPlayer with parameters playerShooting and playerShot, you’d be able to have more versatility with it, when it’s used, etc. It also looks better as your code doesn’t become one huge block.

With your second example, however, you get the ability to use OOP I believe with self. This means you’ll be able to do things like self.Size, self.Health and you’d be creating a class but that’s a whole other can of worms. If I’m wrong on this, however, be sure to notify me and I’ll change it accordingly.

3 Likes

you could also do

function name()

end

EDIT: adding “local” makes the function local and without adding “local” makes the function global

3 Likes

To elaborate on what he means by local and global, he is talking about the scope.

In lua, you can define values that are either global to the whole script or just local to the current scope, with the second saving more memory and generally being better code practice.

For example:

I have a function add(n1, n2)
It defines a local variable for the result and then returns it.

‘local result = n1 + n2’
‘return result’

If I try to access the result value outside of the scope (which is the function) the value will be ‘nil’. That is because it was only defined in the function, with the keyword local so other scopes cannot access it, for the sole reason because it is not needed.

Another example:
We have a function update()
It will run some code to update some values in the code

Before defining this function, we defined three values: name, amount, id

Since it is not in any sub scope, it is automatically “global”, so adding local before it wouldn’t make a difference except for the good practice thing.

Code would look as follows:
local name, amount, id

local function update()
— run code to update values
end

update()
print(name)

(I’m on mobile so I cannot format)

As you see, I am accessing the values outside of the function scope because it was defined on the same scope as the print statement.

3 Likes

Pretty sure there isn’t a difference, the former is just syntax sugar for the latter. You can only use self if you use method syntax (i.e function table:name)

2 Likes