< previous page page_1142 next page >

Page 1142
Execution of this call is complete. The value of toPtr is returned to the preceding call.
Call 1: Execution of this call resumes by assigning the returned function value (the address of the second node) to toPtr->link.
1142-01.gif
Execution of this call is complete. Because this is the nonrecursive call, the value of toPtr is returned to the assignment statement containing the original call. The variable newListHead now points to a clone of the original list.
1142-02.gif
Recursion or Iteration?
Recursion and iteration are alternative ways of expressing repetition in a program. When iterative control structures are used, processes are made to repeat by embedding code in a looping structure such as a While, For, or DoWhile. In recursion, a process is made to repeat by having a function call itself. A selection statement is used to control the repeated calls.
Which is better to use: recursion or iteration? There is no simple answer to this question. The choice usually depends on two issues: efficiency and the nature of the problem being solved.
Historically, the quest for efficiency, in terms of both execution speed and memory usage, has favored iteration over recursion. Each time a recursive call is made, the system must allocate stack space for all (automatic) local variables and actual parameters. The overhead involved in any function call is time-consuming. On early, slow computers with limited memory capacity, recursive algorithms were visibly-sometimes painfully-slower than the iterative versions. However, studies have shown that on modern, fast computers, the overhead of recursion is often so small that the increase in computation time is almost unnoticeable to the user. Except in cases where efficiency is absolutely critical, then, the choice between recursion and iteration more often depends on the second issue-the nature of the problem being solved.
Consider the factorial and power algorithms we discussed earlier in the chapter. In both cases, iterative solutions were obvious and easy to devise.

 
< previous page page_1142 next page >