Recursive Functions
ACSL recursive function problems give you a mathematical definition and ask you to evaluate it for a specific input. The key skill isn’t understanding recursion conceptually - it’s tracing calls quickly and accurately without losing your place.
How ACSL recursive problems work
Every recursive function has two parts:
- Base case(s) - a condition where the function returns a value directly
- Recursive case(s) - the function calls itself with different arguments
Example:
To find , you trace downward until you hit a base case, then work back up.
The trace-down, compute-up method
This is the most reliable technique. Write each call on its own line going down, then fill in values going up.
Find using the function above:
Trace down:
Compute up:
Answer: 31
Speed tip: Write the trace as a neat column. Sloppy work is the #1 source of errors on recursive function problems.
Try it: trace a recursive function
Step through the evaluation of where if , else .
1f(x) = 2·f(x-1) + 1 (if x > 0)2f(x) = 3 (if x is 0 or less)34Trace down (expand until base case):5 f(3) = 2·f(2) + 16 f(2) = 2·f(1) + 17 f(1) = 2·f(0) + 18 f(0) = 3 [base case]910Compute up (substitute values back):11 f(0) = 312 f(1) = 2·3 + 1 = 713 f(2) = 2·7 + 1 = 1514 f(3) = 2·15 + 1 = 31| Name | Value |
|---|---|
| x | 3 |
Trace down: expand each call
Multi-branch recursion (Fibonacci-style)
Some functions call themselves more than once. These are trickier because the call tree branches.
Example:
For , build a table from the bottom up instead of tracing down:
Answer: 8
Key insight: For multi-branch recursion, always build bottom-up. Tracing top-down creates a tree that’s easy to mess up. Bottom-up is a simple table.
Multi-condition functions
ACSL loves functions with 3+ conditions. Read them carefully - the conditions determine which rule to apply at each step.
Example:
Find :
Now compute up:
Answer: 166
Watch out: Notice how the rule changed at . The most common mistake is applying the wrong rule when crossing a boundary.
Two-parameter functions
These appear at higher difficulty levels. Track both parameters carefully.
Example:
Find :
Compute up:
Answer: 9
Indirect recursion
Occasionally ACSL gives two functions that call each other.
Example:
Find :
Compute up:
Answer: 3
Common mistakes
- Applying the wrong condition. Always re-check which branch applies for the current value of x before computing.
- Arithmetic errors during compute-up. Double-check each line - one wrong value cascades through everything above it.
- Miscounting with multi-branch. Use the bottom-up table method for Fibonacci-style problems. Don’t try to trace the tree in your head.
- Forgetting that base cases can be different. and are two separate base cases.
Contest strategy
- Simple single-recursion: ~30 seconds
- Multi-condition: ~60 seconds (be methodful)
- Multi-branch (Fibonacci): ~45 seconds with bottom-up table
- Two-parameter: ~60 seconds (track both values carefully)
- When in doubt, write it out. Mental math causes mistakes here.