0

I have trouble understanding the code of this recursive function. I am new to DART programming. I understand what a recursive function accomplishes, but I have a problem understanding the programming syntax.

int sum(List<int> numberList, int index) {
  if (index < 0) {
    return 0;
  } else {
    return numberList[index] + sum(numberList, index - 1);
  }
}

main() {
  // Driver Code
  var result = sum([1, 2, 3, 4, 5], 4);
  print(result);
}

Question: where is the value for each step stored- Does the result for the first pass at line 5 equals 9 taken the inputs from line 11. Where is the value of result 9 stored? How does the function know to add 9 + 3 in the second pass?

Does the recursive function have "internal memory" of the values generated by each pass?

My understanding of the programing language would be that var result passes the arguments to the sum function.

The sum function executes the if-else command until the index value is 0, which means it executes 4 times. With the first pass the return command creates a value of 9 (5 + 4 since value of index is 5 and value of index-1 is 4).

Here begins my confusion. The sum function would now do a second if-else pass and execute the return command again.

Now the initial value of numberList[index] would need to be 9 and the value of sum(numberList, index - 1); would need to be 3, to get 9 + 3 = 12. Additional 2 passes gets 12 + 2 = 14 and 14 + 1 = 15 the expected result.

My question here is how does (if it does) the index value in the "numberList[index]" changes. The index value is defined as 4. Is this an internal logic of the recursive function or am I completely misinterpreting the programming syntax? I would expect that we have a "temporary" variable for the result which increases with each pass.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • 2
    Does this answer your question? [Understanding how recursive functions work](https://stackoverflow.com/questions/25676961/understanding-how-recursive-functions-work) . Recursion works the same in Dart as many other languages. – julemand101 May 08 '21 at 15:06
  • Thanks but not quite, i understand the concept of recursion but not the programing syntax , I have edited my question – Emil Perić May 08 '21 at 21:08
  • i have created a new Q as this one was closed. https://stackoverflow.com/questions/67452230/problem-of-understading-the-programing-syntax-of-dart-recursion – Emil Perić May 08 '21 at 21:17
  • 1
    Your misunderstanding here isn't specific to Dart. Recursive functions in other languages would operate the same way. "Now the initial value of numberList[index] would need to be 9": This is wrong. `numberList` is never modified. `numberList` is always `[1, 2, 3, 4]` and will store no other values. Temporary values generated as the result of evaluating subexpressions are stored somewhere (generally in a register or on a call stack if no registers are available), and when the recursion completes, all of those values will be added together. – jamesdlin May 08 '21 at 21:33
  • jamesdkin, thanks this explains it. Just another Q, in numberList[index], does the index have value 4 not [1,2,3,4] as the latter are the List values? The index value represents the position. What does "return numberList[index] + sum(numberList, index - 1);" line of code exactly do. Does it do 5 + 4 as the numberList[index] intialy represents position 5 in the list which is value 5 and the sum(numberList, index-1) represents 4 as the index-1 is representing position 4 in the list which has value of 4. – Emil Perić May 09 '21 at 10:32
  • @EmilPerić `return numberList[index] + sum(numberList, index - 1);` is handled so that it will first evaluate `numberList[index]`, saves this result in the stack, then evaluate `sum(numberList, index - 1)`. When this method then returns, the result is saved on the stack. Then the `+` operator are called on the first result against the second result (both values comes from removing them from the stack). – julemand101 May 09 '21 at 16:51

0 Answers0