CMPS 1371 Introduction to Computing for Engineers RECURSION 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 Recursion Examples: Factorial n! = (n)(n-1)(n-2)...(3)(2)(1) Fibonacci Fn = Fn-1 + Fn-2 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… 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 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. Iteration for sum function function result = sum1(x) for i=1:length(x) result = x(i) + result; end Adds each element to result 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) Recursion for sum function function result = sum2(x) if ( isempty(x) ) Calls the function over again till it result = 0; reads all elements elseif ( length(x) == 1) result = x; else result = x(1) + sum2 (x(2:end)); end Factorial Function Create a function to calculate the factorial amount Result should be as follows: >> fact(5) ans = 120 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? Palindromes Palindrome is a word or sentence that is spelled the same forwards and backwards Examples: eye level kayak radar racecar toot i prefer pi Napolean's classic lament: able was i ere i saw elba 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 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 Recursion Characteristics There are three necessary characteristics of all recursive functions 1. 2. 3. There must be a terminating condition to stop the process The function must call a clone of itself 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