Function Calling (with Syntactic Sugar)

An overview of Lua (and as an extension, Luau's) function calling. Taking a look at some lesser known ways of calling a function.

by Cens_r

Author Avatar

This tutorial starts with the impression that you already know how functions work, this isn't a tutorial on functions as a concept. Its meant to showcase some interesting ways you can call functions that go wildly unknown among the Lua community.


Functions are a core part of Lua, learning the ins and outs of them is core to your journey through scripting. They are a huge part in your scripts, not only by breaking things down into more managable portions, but also keeping said scripts organized. Functions should be an avenue for expression the intentions of a portion of code, giving meaning beyond comments and variable names.

This same expression can also bleed over into how you call the functions. Although typically, for other readers sake you want to follow conventions to make it easy to navigate and understand your scripts. On rare occassions you might want to treat a function similar to that of a keyword, which using standard function calling practice isn't really possible. Take the below function for example that takes in a single string:

-- Simple require-by-string function
local function import(moduleName)
	local module = ReplicatedStorage:FindFirstChild(moduleName)
	if module then
		return require(module)

Using standard practices and basic knowledge of functions we're limited to calling this function using:

local MyModule = import("MyModule")

There's nothing inheritly wrong with this, and like I said previously, this is how you really should be using functions. But this tutorial isn't about what should be done, its what you CAN do.

Losing the Parentheses:

Taking a look at our import function, it'd be nice if we could lose the parentheses and treat it similar to a keyword. This is in fact very much possible in Lua, and its as simple as just removing them haha.

-- This is a perfectly valid way of calling the function
local MyModule = import "MyModule"

While achieving the same purpose and functionality I personally find this much more appealing. It is not without its restrictions though.


There are two main restrictions behind this way of calling functions...

  1. Functions can only take in one argument:
print "Hello World" -- Valid
print "Hello" "World" -- Invalid: ERROR
print "Hello", "World" -- Invalid: ERROR
  1. Argument must be either a string constructor or table constructor:
-- Valid Arguments:
print "Hello World"
print { 1, 2, 3, 4, 5 }

-- Invalid Argument: (ERROR)
local x = "Hello World"
print x

-- Invalid Argument: (ERROR)
local y = { 1, 2, 3, 4, 5 }
print y

This does limit the usecases for this pretty severely, but in a good way. If you have any other information you need to pass to a function its much better to contain it in the parentheses so that its clear where arguments begin and end.


I hope you learned something you might find useful! I haven't had all that many usecases for this neat little syntax trick; I am currently using it in my Lua OOP module to create classes using a class keyword. If you guys have any cool ideas for other uses of this feel free to share them! Like I tried to heavily imply this in no way should be used frequently throughout your scripts. While its good to express yourself through your code its also important to make sure its easy to read incase you or someone else comes back later.

This is my first tutorial on here! Thank you for reading. 👋 Censor

View in-game to comment, award, and more!