Jump to content

[Doubt] local function and function


Mature

Recommended Posts

Hello community, I have the following question, what is the difference between a local function and a function?

Example:

local function ()

For this

function ()
  

When should I use a local function and when should I use a common function?

Edited by Hazardinho
Link to comment
  • Moderators
47 minutes ago, Hazardinho said:

When should I use a local function and when should I use a common function?

A local function is just the function value saved inside of a local variable.

 


 

Like this (a local available before the function is defined):

local test

function test ()
  
end

 

or this (a global, as well as a local after the local keyword):



function test ()
  
end

local test = test

 

or this: (a local variable, available after the function)


local test = function ()
  
end

 

or this: (a local variable, available in and after the function)

local function test()
  
end

 

So when do you need to use it? The same way as just a regular local variable.

  • Variables that are accessed a lot of time. (onClientRender)
  • Variables that are only available within it's scope.
  • Variables that are temporary required.
  • etc.

 

 

 

 

Edited by IIYAMA
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...