Jak92

Untitled

Jul 5th, 2020
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.06 KB | None | 0 0
  1.  
  2. -- regular function
  3.  
  4. function sayHello (recipient)
  5.     print('Hello, '..recipient)
  6. end
  7.  
  8. sayHello('Roberto')
  9.  
  10.  
  11.  
  12.  
  13. -- anonymous function (exactly the same as above)
  14.  
  15. local sayHello = function (recipient)
  16.     print('Hello, '..recipient)
  17. end
  18.  
  19.  
  20.  
  21.  
  22. -- function on an object
  23.  
  24. local cat = {}
  25.  
  26. function cat.sayHello ()
  27.     print('Meow')
  28. end
  29.  
  30. cat.sayHello();
  31.  
  32.  
  33.  
  34.  
  35. -- self-executing anonymous function
  36.  
  37. (function (recipient)
  38.     print('Hello, '..recipient)        --> Hello Luiz
  39. end)('Luiz')
  40.  
  41.  
  42.  
  43.  
  44. -- Multiple return values
  45.  
  46. function getCoordinates()
  47.     return 12, 55, 123
  48. end
  49.  
  50. local x, y, z = getCoordinates()
  51. print(x, y, z)                     --> 12  55  123
  52.  
  53.  
  54.  
  55.  
  56. -- varargs
  57.  
  58. function sayAllsorts(...)
  59.     print(...)
  60. end
  61.  
  62. sayAllsorts('Lua', 5.1, 'on the Web')   --> Lua 5.1 on the Web
  63.  
  64.  
  65.  
  66.  
  67. -- mix 'n' match varargs
  68.  
  69. function tail (h, ...)
  70.     return ...
  71. end
  72.  
  73. print(tail (1, 2 ,3))      --> 2, 3
  74.  
  75.  
  76.  
  77.  
  78. -- function calls that pass either a single string or single object don't
  79. -- require brackets
  80.  
  81. print 'Hello, Waldemar'
  82. print { foo = 'bar' }
  83.  
  84.  
  85.  
  86.  
  87. -- functions are first class objects
  88.  
  89. function doStuffWithAThing (thing, ...)
  90.     thing (...)
  91. end
  92.  
  93. doStuffWithAThing(print, 1, 2, 3)
  94.  
  95. a = {}
  96. doStuffWithAThing(table.insert, a, 'moo')
  97. print(a[1])
  98.  
  99.  
  100.  
  101.  
  102.  
  103. -- the colon is just syntatic sugar
  104.  
  105. local cat = { name = 'Ruby', food = 'Felix' }
  106.  
  107. function cat.getName (self)     -- there is no such thing as "self", you have to
  108.                                 -- provide it
  109.     return self.name
  110. end
  111.  
  112. print(cat.getName(cat))         -- pass the cat in
  113.  
  114. -- use a colon instead and Lua will automatically pass what's before the colon
  115. -- as the first argument
  116. print(cat:getName())            -- this is exactly the same as the previous call
  117.  
  118.  
  119. -- better still, you can clean up that function definition with a colon too; Lua
  120. -- will automatically add a "self" parameter at the front
  121. function cat:getFood ()         -- there is no such thing as "self", you have to
  122.                                 -- provide it
  123.     return self.food
  124. end
  125.  
  126. print(cat:getFood())
  127. print(cat.getFood(cat))     -- both these calls are the same.
Add Comment
Please, Sign In to add comment