# Thread: Weird Recursive Function :: C++

1. ## Weird Recursive Function :: C++

Hi.

I am having trouble getting a recursive function to work right.

Here is the criteria:

-----
Variables: X, Y

if X == 0
return 2X

if X>= 1 && Y == 0
return 0;

if X >= 1 && Y == 1
return 2;

if X >= 1 && Y >= 2
return return calculateAck(X, calculateAck(X, Y - 1)
-----

Here is the code:
-----

int CAckermann::calculateAck(int randNumX, int randNumY){ if (randNumX == 0) return (2 * randNumY); else if (randNumX >= 1 && randNumY == 0) return 0; else if (randNumX >= 1 && randNumY == 1) return 2; else if (randNumX >= 1 && randNumY >= 2) return calculateAck(randNumX - 1, calculateAck(randNumX, randNumY - 1));}

-----

The code above works except when it executes the last else-if statement.

Am I doing something wrong? Do I need to deincrement one of the variables before passing it into a function?

Thanks,
Kuphryn

2. Why do you decrement x when you pass it to the recursive function. It isn't in your pseudo code