Problem With Lua Function Usage!

Ok! So I have been trying Lua for 2 days now I don’t understand something! In the Lua interpreter the following code doesn’t work . Why Not?

\>function accept() print "Hello" end \>accept() Hello \>accept( \>\>) Hello \>accept \>\>() stdin:2: ambiguous syntax (function call x new statement '(' )

Also I have one more problem!

Why is this happening?

\>print (2) 2 \>print 2 error!! \>print ("Hello World") Hello World \>print "Hello" Hello \>print (a) nil \>print a error!
\>accept \>\>() stdin:2: ambiguous syntax (function call x new statement '(' )

This error is because the brackets of the function are on a separate line. It thinks you are referring to ‘accept’ as a variable - that doesn’t exist so it returns nil, then looks for something else to execute and all you’re giving it are a pair of brackets.

Why is this happening?

You’re not being very specific here, unless you want a complete explanation of everything in the following block.

I’ll attempt the obvious bits…

\>print 2 error!! \>print a error!

Error because functions really should be called with parenthesis. You can call without them, but there needs to be a denoting element and as numerals and variables can be evaluated in other ways, leaving out the braces just isn’t cricket. Whatever the reason, the formal notation is to use braces when calling functions, so just stick with that.

as per @horacebury

>> the formal notation is to use braces when calling functions

lua offers several “syntactic sugar” typing-shortcuts for function calls, but you’re “mistreating” them a bit.

both of your issues (linefeed before open-parenthesis, quotes as sugar for parenthesis-quote-pair) are documented, by the way.

suggest you either:

  read this entire section, if you want to continue being “tricky”:  https://www.lua.org/manual/5.1/manual.html#2.5.8

OR

  refrain from trying to be so “tricky” and just use “common” syntax, fe: accept() or print(“hello”) or print(a)

\>accept \>\>() stdin:2: ambiguous syntax (function call x new statement '(' )

This error is because the brackets of the function are on a separate line. It thinks you are referring to ‘accept’ as a variable - that doesn’t exist so it returns nil, then looks for something else to execute and all you’re giving it are a pair of brackets.

Why is this happening?

You’re not being very specific here, unless you want a complete explanation of everything in the following block.

I’ll attempt the obvious bits…

\>print 2 error!! \>print a error!

Error because functions really should be called with parenthesis. You can call without them, but there needs to be a denoting element and as numerals and variables can be evaluated in other ways, leaving out the braces just isn’t cricket. Whatever the reason, the formal notation is to use braces when calling functions, so just stick with that.

as per @horacebury

>> the formal notation is to use braces when calling functions

lua offers several “syntactic sugar” typing-shortcuts for function calls, but you’re “mistreating” them a bit.

both of your issues (linefeed before open-parenthesis, quotes as sugar for parenthesis-quote-pair) are documented, by the way.

suggest you either:

  read this entire section, if you want to continue being “tricky”:  https://www.lua.org/manual/5.1/manual.html#2.5.8

OR

  refrain from trying to be so “tricky” and just use “common” syntax, fe: accept() or print(“hello”) or print(a)