Presentation is loading. Please wait.

Presentation is loading. Please wait.

CMPS 1371 Introduction to Computing for Engineers RECURSION.

Similar presentations


Presentation on theme: "CMPS 1371 Introduction to Computing for Engineers RECURSION."— Presentation transcript:

1 CMPS 1371 Introduction to Computing for Engineers RECURSION

2 Recursion Simple definition: When a function calls itself Why: Simple reason: often a problem can reduce to exactly the same problem but on a slightly simpler task

3 Recursion Examples: Factorial n! = (n)(n-1)(n-2)...(3)(2)(1)‏ Fibonacci F n = F n-1 + F n-2

4 A "stack" Last one in is first one out (LIFO) Takes the element on top regardless of size Not too hard to get to recent ones… A stack is used for functions to get space for it's arguments, internal data, and it's return location Each call to a function gets the room it needs on the stack ("pushed" onto the stack). Allows calling function over and over…

5 Recursion vs Iteration Iteration for loops and while loops that solve a problem by iterating through steps Recursion functions that call themselves, moving towards one or more base cases

6 Sum function As you may know, finding the sum of a vector in MATLAB is easy—we can use the built-in MATLAB function sum(): << x = [1 2 3]; << sum(x)‏ ans = 6 However, it is an important programming skill to know how to find the sum of a vector (or an array) without using a built-in function, because many languages do not have such a function.

7 Iteration for sum function function result = sum1(x)‏ for i=1:length(x) result = x(i) + result; end Adds each element to result

8 Recursion for sum function There are only three possibilities for a vector x: x is an empty vector, which has the sum 0 x is really just a single element—hence x is already the sum of the vector x x is vector with at least two elements, and can thus be divided into two parts; the first element, x(1) the rest of the vector, x(2:end)

9 Recursion for sum function function result = sum2(x) if ( isempty(x) )‏ result = 0; elseif ( length(x) == 1)‏ result = x; else result = x(1) + sum2 (x(2:end)); end Calls the function over again till it reads all elements

10 Factorial Function Create a function to calculate the factorial amount Result should be as follows: >> fact(5)‏ ans = 120

11 Factorial Recursion Try this: first make a new function “fact” function y = fact(x)‏ if x==1 y=1; else y=x*fact(x-1); end This function calls itself. It’s recursive. How does this work?

12 Palindromes Palindrome is a word or sentence that is spelled the same forwards and backwards Examples: level radar toot i prefer pi Napolean's classic lament: able was i ere i saw elba eye kayak racecar

13 Palindromes Create a function to determine if a word is a palindrome How would we do it recursively? If start and end are the same and the middle is a palindrome, then is true What are the terminating conditions? if length == 1, yes if length == 2, then if strcmp(str(1),str(2)), then yes

14 Palindrome code function ans = isPal(str) % is str a palindrome? % isPal(‘ enter your word ’) str = lower(str); if length(str) < 2 ans = true; disp('your word is a palindrome') elseif str(1) ~= str(end) ans = false; disp('your word is not a palindrome') else ans = isPal(str(2:end-1)); end

15 Recursion Characteristics There are three necessary characteristics of all recursive functions 1. There must be a terminating condition to stop the process 2. The function must call a clone of itself 3. The parameters to that call must move the function toward the terminating condition A recursive function does not really call itself because it passes different parameters to the function


Download ppt "CMPS 1371 Introduction to Computing for Engineers RECURSION."

Similar presentations


Ads by Google