I was thinking about recursive functions. Take a simple function, for example one to print a linked list recursively:
void print(list *list){
if(list){
cout << list->data
print(list->next);
}
}
At first this seems like a pretty innocuous function, but isn't it storing an address (labeled by the variable list) in every stack frame? Assume there is no tail-call optimization. We need space for N addresses, where N is the size of the list. The space needed grows linearly in proportion to the size of the list.
I can't think of how you would implement a recursive function without having at least one local variable or argument stored on the stack. So, it seems as though every recursive function is going to have at best linear space complexity. If this is the case, then wouldn't it almost always be advisable to use iteration over recursion?