Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Optional function arguments

Asked by
Trewier 146
10 years ago

How would I program optional arguments for a function?

1 answer

Log in to vote
1
Answered by
User#2 0
10 years ago

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.

0
Thanks this is exactly what I needed! Trewier 146 — 10y
Ad

Answer this question