Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Perkin

macrumors newbie
Oct 22, 2013
16
0
The SumNumbersTo function is calling itself until the N variable is 0, then it returns, RESUMING the statements of the functions that haven't yet been reached because of the interruption of the call to the next SumNubersTo

say for example we labelled the lines A -> H
Code:
A    printf ("Pre if loop (N) (sum) %3lu, %3lu\n", N, sum); // debug print
    if (N == 0) {
B        printf("If loop N = 0, N = %3lu, sum = %3lu\n", N, sum); // debug print
C        return 0;
    } else {
D    printf("N greater than 0, PreRecurse sum value = %3lu\n", sum); // debug print
E    sum = N + SumNumbersTo (N-1);  // Look recursion here!
F    printf ("Recurse N = %3lu, sum = %3lu\n", N, sum); // debug print
    }
    
G   printf ("Sum numbers from 1 to %3lu = %3lu\n", N, sum);
H   printf ("N = %3lu, sum = %3lu\n", N, sum); // debug print
    return sum;

The if the variable 'N' > 0 then A,D,E are executed, and the function is then interrupted by the call to a new SumNumbersTo and A,D,E are exeuted again and again unless 'N' = 0, When that happens, then A,B,C are executed, and then the function returns, causing the previous functions statements to be resumed, thus causing F,G,H to be executed several times as the function keeps then returning through the recursed functions

If that helps :confused:
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
It does a lot that is 'magic' at my current level, and uses a rather advanced syntax for the given level of noobness.
Any sufficiently advanced technology is indistinguishable from magic.

I have to disagree, though. ALL you need to know is making function calls. It is not too advanced. You're most of the way there.
After the recursions, it has x number of calls. N == 0, so it issues a return that drops to the next statement after the recursive call and hits the printf, exits the if/else and does those last lines,
Sounds pretty good so far.

flips back to the if/else, returns to the line after the recursion, etc, until it's done and blows out of the function, and ends.
Uh oh. Earlier you noted that a return returns from the function and resumes execution after the function call in the caller. It's the same here. Each call returns to the spot it was called. There's no flipping anywhere.


Big magic, and not too clear what's going on, and the breakpoints don't help as they obliterate/obfuscate the flow...

Warm?

No magic. The breakpoints may not be helping, but they're not obliterating anything. Try something new.

Code:
int main() {
  int result = functionA(4);
}

int functionA(int input) {
  printf("In A with %d", input);
  if(input == 0) {
    printf("Returning 0 from A");
    return 0;
  }
  int resultA = input + functionB(input -1);
  printf("End of A, result: %d", resultA);
  return resultA;
}


int functionB(int input) {
  printf("In B with %d", input);
  if(input == 0) {
    printf("Returning 0 from B");
    return 0;
  }
  int resultB = input + functionC(input -1);
  printf("End of B, result: %d", resultB);
  return resultB;
}

int functionC(int input) {
  printf("In C with %d", input);
  if(input == 0) {
    printf("Returning 0 from C");
    return 0;
  }
  int resultC = input + functionD(input -1);
  printf("End of C, result: %d", resultC);
  return resultC;
}

int functionD(int input) {
  printf("In D with %d", input);
  if(input == 0) {
    printf("Returning 0 from D");
    return 0;
  }
  int resultD = input + functionE(input -1);
  printf("End of D, result: %d", resultD);
  return resultD;
}

int functionE(int input) {
  printf("In E with %d", input);
  return input;
}

Run that, and trace what's happening. Note that each call is causing the caller to wait for the function it called to finish. Make a drawing. When you're in E, main is waiting on functionA, that's waiting on function B, and so on. When what you called returns, you keep running from that point. See if that helps.

-Lee
 

SDDave2007

macrumors regular
Apr 12, 2007
197
1
Ok..... I got lost reading all the replies... but what *I* see in the OP is this

sum = N + SumNumbersTo (N-1); // Look recursion here!

where SUM which is global is being OVERWRITTEN by each recursive call

So at no time is it accumlative
 

ArtOfWarfare

macrumors G3
Nov 26, 2007
9,563
6,062
Ok..... I got lost reading all the replies... but what *I* see in the OP is this

sum = N + SumNumbersTo (N-1); // Look recursion here!

where SUM which is global is being OVERWRITTEN by each recursive call

So at no time is it accumlative

Except it is. It's very weird that sum was declared a global and that leads to a lot of easy potential mistakes where a bug could break this code, but it works. SumNumbersTo will return with the value that the global sum was set to when it finished.
 

subsonix

macrumors 68040
Feb 2, 2008
3,551
79
Taking a week off to soak my brain in alcohol and prog rock helped me to see that this program is pretty complex for a noob.

If you are stuck on this, it's safe to ignore for now and move on, and then come back later to see if it makes more sense imo. It's not something that is a requirement to move on and learn Objective-C and Cocoa. Recursion is more of a generic CS concept.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.