It depends on your data structure and your algorithm. The performance differences on a simple array will be minimal. Then you would want to use whatever makes the code most obvious to the reader, at least in the initial version. Once you have it working, you would use a debugger to find the slow bits and just improve them.

When you start using linked lists and tree structures then recursion makes more sense. There are some other mathematical functions where it is also useful such as working out the factorial of a number.

In a linked list, or tree, each object points to the next one or more objects. This next bit is from wikipedia:

Code:

struct node
{
int data; // some integer data
struct node *next; // pointer to another struct node
};

Because the struct node data structure is defined recursively, procedures that operate on it can be implemented naturally as recursive procedures. The list_print procedure defined below walks down the list until the list is empty (i.e., the list pointer has a value of NULL). For each node it prints the data element (an integer). In the C implementation, the list remains unchanged by the list_print procedure.

Code:

void list_print(struct node *list)
{
if (list != NULL) // base case
{
printf ("%d ", list->data); // print integer data followed by a space
list_print (list->next); // recursive call on the next node
}
}

Performing the above on a linked list with iteration would be slow, and you would probably have some similar code hidden somewhere anyway. Worst case, you might end up recursively going through the list each time you try and print an object. That would be relatively inefficient, see the next code block and imagine calling that in a 'for each' statement for each element in the list/array. Its important to understand the data structures your working on.

Code:

int list_index(struct node *list, int i)
{
if (list != NULL) // base case
{
if (i == 0) {
return (list->data);
}
return list_index(list->next, i - 1); // recursive call on the next node
}
}
int list_length(struct node *list, int i = 0)
{
if (list != NULL) // base case
{
return list_length(list->next, i + 1); // recursive call on the next node
} else {
return i;
}
}
for (int c = list_length(list, 0); 0; c ++) {
printf("%d ", list_index(list, c));
}