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...)
Resources
Section titled “Resources”Exercises
Section titled “Exercises”morefn1 test
Section titled “morefn1 test”morefn1 — recursion
Show hint
Recurse with a base case:
if n <= 1 { return 1 }return n * factorial(n-1)morefn2 test
Section titled “morefn2 test”morefn2 — variadic functions
Show hint
A variadic parameter is a slice inside the function:
for _, n := range nums { total += n }