Here is an example of recursion.
Code:
void DrawGrid(int iteration,int x,int y,int x2,int y2)
{
if ((x2-x)<=1) return;
int midx=(x2+x)>>1;
int midy=(y2+y)>>1;
color=random(255);
Box(x,y,x2,y2,color);
DrawGrid(iteration-1,x,y,midx,midy);
DrawGrid(iteration-1,midx,y,x2,mid);
DrawGrid(iteration-1,x,midy,midx,y2);
DrawGrid(iteration-1,midx,midy,x2,y2);
}
void Box(int x,int y,int x2,int y2,int color)
{
//Draw a box
}
Slow the code down and you will begin to understand what it is doing and hopefully what recursion is all about. There are times when it is very useful, and there are times when it should be avoided like the plague.
Recursion takes up a lot of stack space since every time you call a function, a stack frame is created. Also, this function takes parameters which must be pushed/popped off of the stack which takes time as well. This function should not overflow the stack, but a function like a floodfill most certainly would. So, if stack space is not a problem and if implementing the function iteratively is overly complex then use recursion. Otherwise, go with the iterative approach. Recursion is used in fractals and is also used to generate terrain maps on the fly. The above algorithm could be altered to produce a terrain using the midpoint displacement algo.