A function can call itself. This is recursion. Write a function that solves a small piece and calls itself for the rest. Each call gets its own local variables. Example: int factorial(int n) { if (n <= 1) return 1; return n * factorial(n - 1); }.
It calls down to factorial(1), then results bubble back up. Every recursive function needs a base case where it stops. Without it, infinite recursion and stack overflow. Recursion needs care.