Tutorial: Variable Arguments to Functions

In a previous tutorial, Passing Data To/From Functions, we illustrated how to pass arguments to function calls. In this tutorial we’ll expand upon the concept and outline other ways to handle optional arguments.

Varying Number of Arguments

Sometimes a function may need to accept a variable number of arguments. Lua accommodates for this with the variable arguments (...) syntax. Consider the following function which takes an unspecified amount of numerical arguments and adds them together into one sum:

1

2

3

4

5

6

7

8

9

10

local functionaddNumbers(...)

local sum=0

fori=1,#arg do

sum=sum+arg[i]

end

returnsum

end

local finalSum=addNumbers(1,12,4,5,2)

print(finalSum)

Notice how Lua uses 3 consecutive periods (...) to indicate a varying number of arguments, and note that this is not the special “ellipsis” character (…). Once inside the function, this produces a table called arg which is a numerically-indexed table (array) that can be iterated over to access each of the arguments:

1

2

3

fori=1,#arg do

--dosomething with eachargument(arg[i])

end

Handling Argument Types

In the example function above, we expect each argument to be number, so we handle them accordingly by adding them together. However, we might need to deal with a mixed set of arguments including numbers, strings, tables, or even other functions. To help verify each argument, Lua provides the simple type() function which can be used to detect arguments of a specific type and react accordingly.

Let’s build a custom string.join utility function which can be included as part of the string library. To keep it object-oriented like the other string functions, we’ll pass in the string to append to as the first argument. We’ll also pass in a separator string to make the returned value more readable.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

functionstring.join(...)

--functionrequiresastring,aseparator,andat least one argument;

--ifthiscondition isnotmet,print anderror andreturnnil

if(#arg &lt; 3 ) then

print("Error: required string and separator missing")

returnnil

end

--the stringisthe first argument

local str=arg[1]

--the separator isthe second argument

local separator=arg[2]

--loop from the third index(the first real argument tojoin tothe string)

In this second example, the : colon operator causes the function to pass in itself (cookieIngredients) as the real first argument.

Conclusion

As exhibited, functions which accept a variable number arguments can be a potent tool in your code arsenal. Further enhancing these functions to filter and handle specific type(s) of arguments can be especially useful when dealing with input arguments which are not assured to be of a predictable type or in a particular order.

Disclaimer: SDKNews.com only syndicates the blog entries from various SDK blogs.
We are not the creator/author of these entries (posts). Product names, brand names
and company names mentioned on this site may be trademarks of their respective owners.