Jump to content

Difference between function and local function


Mickoi

Recommended Posts

Hi guys, i came to you with another question. For example, i have main_c.lua and utilities_c.lua and in main_c.lua i create onClientClick event and i need function 'isMouseIn' (check if mouse is in positions...) and i can do it by add code on top of main_c.lua at local function, but i attention when code is clear and i want to create this function in file utilities_c.lua and it will be global function so there is my question. Is that difference is big? local function and global function in other file? 

I hope you can read what i said! Thank you in advance. :)

Link to comment
  • Moderators
1 hour ago, Mickoi said:

local function and global function in other file? 

Function names are variables.

  • local variables are only available within their block.
  • local variables are faster in speed. (performance)

 

Blocks:

  • Local functions can't be called from another file.
  • Local functions can't be called outside of:
    • (other) Function
    • if statement
    • Loop
    • etc.

 

-- file 1
local function test ()
  
end

--



-- file 2

iprint(test) -- nil

--

 

if true then
  local function test ()

  end
end

iprint(test) -- nil

 

function something ()
  local function test ()

  end
end

iprint(test) -- nil

 

Depending on how the function variable is defined:

local function test ()
	iprint(test) -- function...
end
local test = function ()
	iprint(test) -- nil (The function is created, before the variable is available.)
end

iprint(test) -- Function...
local test
test = function  ()
	iprint(test) -- function...
end

 

 

 

 

 

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...