benqbenq
benqbenq
CC#
Created by EmperMiner on 7/26/2023 in #help
❔ Can anyone explain this piece of code
9 replies
CC#
Created by EmperMiner on 7/26/2023 in #help
❔ Can anyone explain this piece of code
If you are talking about the algorithm itself, I don't think I can help you because: 1) recursive functions are hard to track 2) I'm DUM The "ref" keyword means the value is passed by reference: so when some recursive call stop at some point and start going backwards to the initial caller, the intermediate calculated values (with "ref" keyword ones) will be not the same, as they were before stepping into this recursive call. To illustrate, here's an example:
int initialValueToCalculate = 1;
Example(0, ref initialValueToCalculate);
Console.WriteLine(initialValueToCalculate); // 8

static void Example(int currentIteration, ref int someValue)
{
// Stop on 3rd recursion call
if (currentIteration == 3)
{
return;
}

someValue *= 2; // Do calculations that will be shared in all recursion steps
Example(++currentIteration, ref someValue);
}
int initialValueToCalculate = 1;
Example(0, ref initialValueToCalculate);
Console.WriteLine(initialValueToCalculate); // 8

static void Example(int currentIteration, ref int someValue)
{
// Stop on 3rd recursion call
if (currentIteration == 3)
{
return;
}

someValue *= 2; // Do calculations that will be shared in all recursion steps
Example(++currentIteration, ref someValue);
}
9 replies