Explanation step-by-step:
-
Function Definition:
This defines a function called printArr that takes:
arr: a list of elements
index: the current position in the list to print
-
Base Condition (to stop recursion):
This checks if the index is still within the bounds of the list. If not, the function stops (ends recursion).
-
Print the current element:
This prints the element at the current index.
-
Recursive Call:
This calls the function again, with index+1, to print the next element.
Sample Execution:
Given:
Here's what happens:
Call | Output |
---|---|
printArr(arr, 0) | 1 |
printArr(arr, 1) | 3 |
printArr(arr, 2) | 5 |
printArr(arr, 3) | 7 |
printArr(arr, 4) | (stops, since 4 >= len(arr)) |
✅ Output:
0 Comments:
Post a Comment