syntax.us Let the syntax do the talking
Blog Contact Posts Questions Tags Hire Me

Question:
In Lua how to default function arguments?

In Lua the syntax to default function arguments is different than what I see in Ruby and Python.

In Lua the feature happens below the parenthesis, not inside the parenthesis (like we have in Ruby and Python):

dan@hp ~/ks/b/lua $ 
dan@hp ~/ks/b/lua $ cat default_arg.lua
-- ~/ks/b/lua/default_arg.lua

function hello(arg)
  -- Default value of arg is 'world':
  arg = arg or 'world'
  return 'hello '..arg
end

print(hello())
print(hello('Dan'))
dan@hp ~/ks/b/lua $ 
dan@hp ~/ks/b/lua $ lua default_arg.lua
hello world
hello Dan
dan@hp ~/ks/b/lua $ 
dan@hp ~/ks/b/lua $ 


syntax.us Let the syntax do the talking
Blog Contact Posts Questions Tags Hire Me