Recursion

Hello,
I was working on a practice problem from an online source (cprogramming.com). The problem was to calculate any given position on Pascal's Triangle and print out the number. Evenatually, I viewed their solution. This function is confusing to me:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int compute_pascal(int row, int position)
{
	if(position == 1)
	{
		return 1;
	}
	else if(position == row)
	{
		return 1;
	}
	else
	{
return compute_pascal(row-1, position) + compute_pascal(row-1, position-1);
	}
}


This line is the source of the confusion:
return compute_pascal(row-1, position) + compute_pascal(row-1, position-1);

I do understand recursion, but this I can't wrap my head around.
Thanks for your help.

The URL for the problem:
http://www.cprogramming.com/challenges/pascal.html
Think of it not like a triangle but as stairs

    1  2   3  4  5  6
1   1
2   1  1
3   1  2   1
4   1  3   3  1
5   1  4   6  4  1
6   1  5  10 10  5  1

Now you can see that position is 1 in any row the answer is 1. This is the first if check. 
You can see that at the end of each row the value is 1 and the number of positions is equal to the row number, so this is if check number two. 
And the else, if you follow the steps up you can see that each  number (not on the ends) depend on the two numbers in the previous row.  The one directly above in the same position  [ compute_pascal(row-1, position) ] and the number before that number [compute_pascal(row-1, position-1) ]. That dependence is the sum of those two numbers. 
Topic archived. No new replies allowed.