Optional Arguments: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
mNo edit summary
Line 1: Line 1:
[[Optional Arguments]] are arguments that are passed to a function but are not required for the function to run. Often, if you do not specify them, default values will be used instead.
[[Optional Arguments]] are arguments that are passed to a function but are not required for the function to run. Often, if you do not specify them, default values will be used instead.


When loolking at the Syntax for an argument, Optional arguments are always enclosed in Square brackets.
When looking at the Syntax for an argument, Optional arguments are always enclosed in Square brackets.


If you do not want to specify the previous arguments, use "nil", which is what tells LUA not to assign any value to the argument.
If you do not want to specify the previous arguments, use "nil", which is what tells LUA not to assign any value to the argument.
Line 7: Line 7:
Example:
Example:


<syntaxhighlight lang="lua">vehicle createVehicle ( model, x, y, z, [rx, ry, rz] )</syntaxhighlight>
<syntaxhighlight lang="lua">vehicle = createVehicle ( model, x, y, z, [rx, ry, rz] )</syntaxhighlight>


In this example, '''rx''', '''ry''', and '''rz''' are [[Optional Arguments]].
In this example, '''rx''', '''ry''', and '''rz''' are [[Optional Arguments]].

Revision as of 11:06, 18 May 2007

Optional Arguments are arguments that are passed to a function but are not required for the function to run. Often, if you do not specify them, default values will be used instead.

When looking at the Syntax for an argument, Optional arguments are always enclosed in Square brackets.

If you do not want to specify the previous arguments, use "nil", which is what tells LUA not to assign any value to the argument.

Example:

vehicle = createVehicle ( model, x, y, z, [rx, ry, rz] )

In this example, rx, ry, and rz are Optional Arguments.

Using Optional Arguments

Optional Arguments have one limitation. You cannot use any optional arguments unless all previous arguments are also supplied.

This means that in the previous example, if you wanted to supply rz, you would also need to supply rx, and ry in order.