Presentation is loading. Please wait.

Presentation is loading. Please wait.

General Computer Science for Engineers CISC 106 Lecture 09 Dr. John Cavazos Computer and Information Sciences 03/04/2009.

Similar presentations


Presentation on theme: "General Computer Science for Engineers CISC 106 Lecture 09 Dr. John Cavazos Computer and Information Sciences 03/04/2009."— Presentation transcript:

1 General Computer Science for Engineers CISC 106 Lecture 09 Dr. John Cavazos Computer and Information Sciences 03/04/2009

2 Lecture Overview Recursion ◦ -Definition ◦ -Characteristics ◦ -Details ◦ -Examples

3 Recursion Definition Definition ◦ The process where a function can call itself. Characteristics ◦ Powerful technique ◦ Allows breaking big problems into smaller and smaller problems. ◦ One must ensure recursion stops

4 When you call a function… Matlab creates a ‘record’ to store the variables from the new function ◦ Record is pushed on the “stack” When function done, result passed back to calling function ◦ Record is popped off stack.

5 When you call a function… function foo1 function bar1 function foo2 function bar2 function main function main calls foo1 function foo1 calls bar1 function bar1 calls foo2 function foo2 calls bar2 function bar2 executing In recursion these would all same function!!

6 Why Can a Function Call Itself? Calling a function creates a an ‘instance’ of the function! When a function calls itself it creates a new ‘instance’ of itself Each instance pushed on stack ◦ At top of stack is instance of function currently executing

7 Recursion requires 3 things Terminating condition to stop! ◦ Force creation of new instances of a function Function must call itself ◦ With different inputs! Terminating condition must eventually be true

8 Example How many CEO’s at this table? A recursive approach…… Taken from http://cache.daylife.com/imageserve/0dgm32v6aYgv6/610x.jpg

9 Example How many CEO’s at this table? A recursive approach…… Taken from http://cache.daylife.com/imageserve/0dgm32v6aYgv6/610x.jpg How many CEOs to my left?

10 Example How many CEO’s at this table? A recursive approach…… Taken from http://cache.daylife.com/imageserve/0dgm32v6aYgv6/610x.jpg I don’t know how many to your left?

11 Example How many CEO’s at this table? A recursive approach…… Taken from http://cache.daylife.com/imageserve/0dgm32v6aYgv6/610x.jpg I don’t know either how many to your left?

12 Example How many CEO’s at this table? A recursive approach…… Taken from http://cache.daylife.com/imageserve/0dgm32v6aYgv6/610x.jpg None…. Just angry senators….

13 Example How many CEO’s at this table? A recursive approach…… Taken from http://cache.daylife.com/imageserve/0dgm32v6aYgv6/610x.jpg It’s just Bob next to me

14 Example How many CEO’s at this table? A recursive approach…… Taken from http://cache.daylife.com/imageserve/0dgm32v6aYgv6/610x.jpg It’s just Bob and Jim next to me

15 Example How many CEO’s at this table? A recursive approach…… Taken from http://cache.daylife.com/imageserve/0dgm32v6aYgv6/610x.jpg Why am I stuck with Bob, Jim and Frank?

16 Example How many CEO’s at this table? A recursive approach…… Taken from http://cache.daylife.com/imageserve/0dgm32v6aYgv6/610x.jpg Just the 4 of us…

17 Another Recursion Example Classic Example ◦ Function output = numbersSum(input) if (input == 1) output = 1; else output = input+numbersSum(input-1) end


Download ppt "General Computer Science for Engineers CISC 106 Lecture 09 Dr. John Cavazos Computer and Information Sciences 03/04/2009."

Similar presentations


Ads by Google