The title might be a little confusing, so let me break this to you.
In Swift, we can have functions with default parameter values, e.g:
func foo(value: Int = 32) { }
And we also can have In-Out parameters, e.g.
func foo(value: inout Int) { }
But, there is a way to have both? A inout parameter, that if it wasn't passed, we can create? This is particularly useful with recursive functions, and I am trying to figure this out.
Default parameters are mostly just a convenient shorthand for overloads. When you want to move beyond the basics, you just build the overload by hand, which allows you any level of complexity you want:
As a side note, be very careful with recursion in Swift. There's no tail-call optimization, so you can blow up the stack if the recursion is deep. The lack of elegant pattern matching makes recursive code much less elegant, and passing values by
inout
makes things harder on the optimzer. In most cases you'll want a loop (or to hide the loop in a higher-order non-recursive function likemap
).Also, don't forget about nesting functions, which is helpful for hiding accumulator-style parameters.