12.07.2015 Views

Wiley-World.of.Warcraft.Programming.A.Guide.and.Reference.for.Creating.WoW.Addons

Wiley-World.of.Warcraft.Programming.A.Guide.and.Reference.for.Creating.WoW.Addons

Wiley-World.of.Warcraft.Programming.A.Guide.and.Reference.for.Creating.WoW.Addons

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

42 Part I ■ Learning to ProgramEmpty ArgumentsTry the following in your interpreter:> print(convert_c2f())stdin:2: attempt to per<strong>for</strong>m arithmetic on local 'celsius' (a nil value)stack traceback:stdin:2: in function 'convert_c2f'stdin:1: in main chunk[C]: ?When no value is passed as an argument, the argument gets the value <strong>of</strong>nil. The first line <strong>of</strong> the convert_c2f function tries to multiply celsius by 1.8<strong>and</strong> errors out because nil can’t be part <strong>of</strong> an arithmetic expression. A similarerror will occur if you pass other non-number values into this function.No Return ValuesNot every function you encounter will have a return statement because notall functions need to return anything. The hello() function defined earlierin this chapter is one such example. In these cases any assignments or otherexpressions involving a call to the function will evaluate to nil. Here’s anexample:> function hello() print(“Hello <strong>World</strong>!“) end> test = hello()Hello <strong>World</strong>!> print(type(test))nilFunctions as Lua ValuesEach function in Lua is just a plain Lua value <strong>of</strong> the type function. Thesevalues can be compared (using == <strong>and</strong> ~=), bound to variable names, passedto functions, returned from functions, <strong>and</strong>usedaskeysintables(tablesareexplored in Chapter 4). A Lua value that is treated this way is called a first-classobject, <strong>and</strong> a language that supports functions in this way is said to havefirst-class functions.Run the following in your interpreter:> hello = function() print(“Hello <strong>World</strong>!“) endThis creates a new function called hello. This value can now be comparedin the same way you’d compare any other Lua value.> print(hello == hello)true> hello2 = hello> print(hello2 == hello)

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!