**Question**

I wish to know if this is a viable way to implement variable depth recursion so that I can run a function at each step and any better/other solutions to the description problem.

**Description**

Suppose I wish to have a function that fills an array either in pattern

`x,y,x,y,x,y`

where x and y are variables defined by some algorithm

and `x,y,z,x,y,z`

where x, y and z are variables defined by the same algorithm.

This should continue for all number of variables. Is this a viable way to implement it.

```
void recurse_n(int n)
{
while(n > 0)
{
--n;
recurse_n(n);
n = 0;
// Use algorithm here
}
}
```

EDIT: Removed the incorrect return type previously referred to. Brainfart.

So, based on your comment you want to know the best way to set up a recursive function. What you have done will work, but it is convoluted and slightly confusing. What I would do to simplify it is:

```
void recurse_n(int n) {
if (n <= 0) {
// Break-out condition
return;
}
--n;
recurse_n(n);
// Your algorithm stuff here.
}
```

That will make it easier to see what is going on. One thing I would add though is that you might want to do the algorithm stuff before the call to recurse_n, but that all depends on what your algorithm is doing.

If you think about it, the way I have written it, it will recurse until n is less than or equal to 0 before doing any of the algorithm work. It might be that you want to do the algorithm work then recurse.