Local Functions Vs Global Functions

Hi, I noticed that you can make local functions and “global” functions, however I had a few questions that weren’t answered by other posts I read.

  1. When people say a global function, it means it can be accessed everywhere in the script regardless of scope, and its’ not being added into _G right?

  2. Creating a global function inside another function would essentially just create a function, however I’m going to assume when you declare that nested function it’ll be saved in memory and won’t be recreated each time you call the function?

  3. What is the “performance” difference if there is one between local functions and global functions, and should I always declare a function as local, unless I need it to be access able regardless of scope?

  4. How do global variables work compared to local variables? Do they doing called “hoisting”, and put the declaration of the variable at the top of the script during runtime or something?

  5. Why is returning local functions through a modulescript or generally returning it not valid?

3 Likes
  1. Yes, it can be accessed in the script anywhere where you share the environment with where the function was defined (which is usually everywhere). Roblox’s _G is just an empty shared table that holds nothing on its own.
  2. It will be created twice and it will overwrite the old one. Luau has optimizations (such as LOP_DUPCLOSURE) in place that attempt to reuse functions, however it only works if the function holds no deeply nested upvalues (locals).
  3. Generally, local functions are always going to be faster. Environment lookups are expensive, while referencing a local function usually means simply moving objects on the stack, or at most just getting an upvalue. Besides, Luau also does inlining only on local functions that it knows you use, so by using global functions you could also be missing out on that performance boost. You can define a local function in the top scope and have it be accessible in every other scope of your script.
  4. Global variables are held in the environment of the function you declare them in (and subsequently every other function which shares that environment). Local variables are always going to be stack references or will remain in an encapsulated form (when the upvalue is closed). In the latter case, this may allow the contents of the locals to GC, which in turn frees up memory. Global variables linger around and could cause potential memory leaks.
  5. I don’t see why it would not be valid (it is quite literally doable). If you were under that impression from what other people have told you, I would personally not go around following random trends that people set for what’s right or what’s wrong when it comes to stuff as basic as returning a function from a module. A module can return absolutely anything (even nil) and has no restrictions for what that value is (as long as there is one).
6 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.