Skip to content

More Functions

Beginner · Fundamentals

Two function techniques beyond the basics.

  • morefn1 — recursion (a function calling itself, with a base case)
  • morefn2 — variadic functions (...int) and spreading a slice (slice...)

morefn1 — recursion

Show hint
Recurse with a base case:
if n <= 1 { return 1 }
return n * factorial(n-1)

Source

morefn2 — variadic functions

Show hint
A variadic parameter is a slice inside the function:
for _, n := range nums { total += n }

Source