Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why does print() work without parenthesis even though it's a function?

These two lines:

print("Hello")
print "Hello"

They both do the same thing: They print the string "Hello." So even though print() is a function, it works as a command/keyword? What's going on here?

2 comments

0

Not sure but I think this is why it still works without parenthesis  : https://www.lua.org/pil/5.html

0

Beforehand ROBLOX’s Lua API allowed recursions of this. Either they’ve forgotten to deprecate this or it’s accepted due to the website posted above.

2 answers

✓ Accepted answer

fn"str" and fn{} only work when passing string/table literals (not in a variable, table element, ect). This is syntax sugar for fn(...). The only downside to the sugar method is that you can only pass one argument to the function.

Examples of code that will fail:

local str, t = "Hello", {}
print str -- # fail
print t -- # fail

print"Hi", true -- # fail

In short:

print "Hello" is sugar for print("Hello").

The function "print ("Hello")" allows you to use Object names and variables such as this(put a part down named "Part" before running the script):

print(game.Workspace.Part.Name)

Without the parentheses the script thinks you are trying to change the value of Part.Name instead of printing it.

1 comment

0

That's not what I'm looking for, I'm looking for WHY it works.

This discussion is preserved as it was. New answers, comments, and votes are closed.