What is the syntax to create the function, but then add it’s implementation further down in code?
So roughly like this:
- Define function
doX - Call
doX(further down in the code) doXimplemention (i.e. all functions down at the bottom of the file)
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I wanted to clarify this point, and I felt that an answer would be the better way than a comment.
Lua is a much simpler language than C or C++. It is built on some simple foundations, with some syntactic sugar to make parts of it easier to swallow.
There is no such thing as a “function definition” in Lua. Functions are first-class objects. They are values in Lua, just like the number 28 or the string literal
"foo"are values. A “function definition” simply sets a value (namely, the function) into a variable. Variables can contain any kind of value, including a function value.All a “function call” is is taking the value from a variable and attempting to call it. If that value is a function, then the function gets called with the given parameters. If that value is not a function (or a table/userdata with a
__callmetamethod), then you get a runtime error.You can no more call a function that hasn’t been set in a variable yet than you can do this:
And expect
additionto have 25 in it. That’s not going to happen. And thus, for the same reason, you can’t do this:As Paul pointed out, you can call a function from within another function you define. But you cannot execute the function that calls it until you’ve filled in that variable with what it needs to contain.