How would I program optional arguments for a function?
✓ ANSWER ACCEPTED
Optional function arguments
1 answer
✓ Accepted answer
It's some-what confusing, if you try to over-complicate it.
With optional arguments, they must be at the end of the function's arguments list.
function SomeFunc(a, b, c)
if a then
print(tostring(a))
end
if b and c then
print(tostring(b)..tostring(c))
end
end
SomeFunc("HI!") --> HI!
SomeFunc("Nothing", "Woot") --> Nothing [`c` wasn't specified, so it defaulted to nil, and would be false in the if statement]
SomeFunc("This", "Does", "Work") --> This (new line) DoesWork
What happens is whenever a function is called and not every argument is passed, it sets those arguments that weren't passed to nil.
This discussion is preserved as it was. New answers, comments, and votes are closed.

1 comment
Thanks this is exactly what I needed!