Bifalgorithm post · Computer science one · Recursion
How can recursive functions be written in C?
How can recursive functions be written in C? (Computer science one · Recursion) — "C supports recursion with no special syntax necessary. However, as a structu…
Ideas in this flowchart
- Recursion in C
- C supports recursion without special syntax.
- Recursion is generally expensive in C, so iterative or other non-recursive solutions are usually preferred.
- Examples of recursive functions
- The countDown function demonstrates a simple recursive countdown.
- Does n equal zero?
- If n equals zero, the function prints “Happy New Year!”
- Otherwise, it prints n and calls countDown with n - 1.
- The recSum function recursively sums the elements of an array.
- recSum receives an array, its size, and an index variable.
- Has the index reached the array size?
- If the index equals the size, the function returns zero as the base case.
- Otherwise, it recursively increments the index and adds the returned result to arr[i].
- The function is invoked as recSum(arr, n, 0), starting the index at zero.