You have:
func test(currentIndex: Int, op: inout [Int]) {
if currentIndex > 1 {
print("exit here**********************")
return
}
print("##before", currentIndex)
test(currentIndex: currentIndex+1, op: &op) //First
print("after------------------------------------------------------", currentIndex)
test(currentIndex: currentIndex+1, op: &op) //Second
print("^^^Final ================ ", currentIndex)
}
var op1: [Int] = []
test(currentIndex: 0, op: &op1)
Let's manually analyze what's doing the last line which launch the test.
We'll copy/paste the code, as we were debugging, if we were manually writing all the lines.
I used indent to keep clarity. It's making the "trace" of your code.
//Index is 0:
if currentIndex > 1 {} //Skipped
print("##before", currentIndex)
test(currentIndex: currentIndex+1, op: &op) //First
//Index is 1
if currentIndex > 1 {} //Skipped
print("##before", currentIndex)
test(currentIndex: currentIndex+1, op: &op) //First
//Index is 2
if currentIndex > 1 {
print("exit here**********************")
return
}
print("after------------------------------------------------------", currentIndex)
test(currentIndex: currentIndex+1, op: &op) //Second
//Index is 2
if currentIndex > 1 {
print("exit here**********************")
return
}
print("^^^Final ================ ", currentIndex)
print("after------------------------------------------------------", currentIndex)
test(currentIndex: currentIndex+1, op: &op) //Second
//Index is 1
if currentIndex > 1 {} //Skipped
print("##before", currentIndex)
test(currentIndex: currentIndex+1, op: &op) //First
//Index is 2
if currentIndex > 1 {
print("exit here**********************")
return
}
print("after------------------------------------------------------", currentIndex)
test(currentIndex: currentIndex+1, op: &op) //Second
//Index is 2
if currentIndex > 1 {
print("exit here**********************")
return
}
print("^^^Final ================ ", currentIndex)
print("^^^Final ================ ", currentIndex)
So you get:
##before 0
##before 1
exit here**********************
after------------------------------------------------------ 1
exit here**********************
^^^Final ================ 1
after------------------------------------------------------ 0
##before 1
exit here**********************
after------------------------------------------------------ 1
exit here**********************
^^^Final ================ 1
^^^Final ================ 0
You can also use the debugger, put a breakpoint, see the callstack...