Lua Logo
Functions

What do we already know about functions? (if we have read the tutorial up to here)
  • They have a name
  • They have to be called explicitly
  • They execute something (if they have been called)
  • They include a chunk (which can also include several chunks again)
  • They will be stopped by "end"
They can also return something. For this we use the key word "return"

return WhatIWantToPassToAnotherPartOfTheScript

It is also possible to return more than one parameter. This will look like:

return WhatIWantToPassToAnotherPartOfTheScript1, WhatIWantToPassToAnotherPartOfTheScript2, WhatIWantToPassToAnotherPartOfTheScript3, .....

  • return has to be always the last key word before end.
  • return finishing the function
  • return this may (however, must not) return a value
  • The value can be of any type we know
  • return may be placed right in the middle of a function (but always before an end)


If the interpreter comes across this:

FunctionsName()

Due to the brackets the interpreter knows that there is a function which has to be executed.

He is looking for this function in his memory and if it exists it will executes its included statement.


If this function is unknown, the interpreter will return an
  • Error: (17) attempt to call global 'FunctionsName' (a nil Value)
What reasons can be responsible for it?
  • She is really not exsiting
  • We misspelled the name
  • The function has not been read of the interpreter until now
By the way: above mentioned (17) in error-message shows the line number in which the error occured. In front of this there is also called the file name in which this happend. In our case:
[string "Unit1.lua"] we haven't renamend our file until now. But everything at the proper time.


We have already learned almost all about functions. But one thing is still missing. We also want to pass values to the function.
Now we come to a new / wellknown word

nach obenParameter

We have already used it in the chapter "Variables in LUA". And we already know that they have to start with an underscore.


Supposed we have a function named "Run".
As parameter we would like to pass
  • Who should run
  • Where should be run
Then we call the function by:

Run("Angus", positionHeadQuarter)

You can see: the parameter we pass to the function are separated by a comma.

The function itself has to look like:

function Run(_who, _where)

When we read the chunk inside the function we always know where passed parameters are located. Due to their underscore. And we are not going to change these underscores later on (never).
Something else: inside functions parameters are always declared as local. Only inside the chunks of the according function they can be seen, changed and called. If the function has been finished they do not exist anymore. They are nil.

Now we have already finished the chapter about functions. All remaining things will be done by the chunk inside the function.

Actually not as difficult as we thought? (sorry, but it will become more difficult later on. Namely, when we are going to combine all what we have learned until now.)
Copying of any content of this site (text or graphics) is not allowed, excepted any source code shown in this tutorial. See also: ....Disclaimer
Copyright © Robert Schmitz 2006