I am trying to write a function to print an array by recursion.
I have written three functions but they seems to be the same in terms of principle.
void printArray0( int theArray[], int theSize, int theIndex = 0 )
{
cout << theArray[theIndex] << ' ';
if ( ++theIndex != theSize ) printArray0( theArray, theSize, theIndex );
else cout << endl;
}
void printArray1( int theArray[], int theElementLeft )
{
cout << *theArray << ' ';
if ( theElementLeft != 1 ) printArray1( theArray+1, theElementLeft-1 );
else cout << endl;
}
void printArray2( int theArray[], int theSize )
{
static int myIndex = 0;
cout << theArray[myIndex] << ' ';
if ( ++myIndex != theSize ) printArray2( theArray, theSize );
else cout << endl;
}
So are there significant differences between them?
If there are, which function is the fastest and which is the safest?
I hope I can learn from others' experience :)