WEEK8 - Week8 ClicktoeditMastersubtitlestyle...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
Click to edit Master subtitle style  3/14/11  Dr. Lale T. Ergene Week 8 Improved building blocks
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
 3/14/11  Dr. Lale T. Ergene Up to now… o We have learned procedures and modules as the  fundamental building blocks o These are somewhat restrictive and can be extended! o Both recursive and non-recursive algorithms can be  passed as argument to the other procedures o It was stated before that wasn’t normally permitted for  a function or subroutine to reference itself, either  directly and indirectly -a concept known as  recursion   22
Background image of page 2
 3/14/11  Dr. Lale T. Ergene What is  recursion ? o Recursion is an important mathematical concept. o Recursion in programming is a technique for  defining a problem in terms of one or more smaller  versions of the same problem. o The solution to the problem is built on the result(s)  from the smaller version(s). 33
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
 3/14/11  Dr. Lale T. Ergene o Best known example: the factorial n! = n (n-1)!  0! = 1  That’s all you need to calculate n! What is  recursion ? 44
Background image of page 4
 3/14/11  Dr. Lale T. Ergene Recursive procedures recursive function function_name(…) result (result) . . end function function_name o Let’s use it to calculate n! o Structure plan 1.  n = 0 factorial_n = 1 2.  n > 0 factorial_n = n * factorial(n-1) 3.  n < 0  55
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
 3/14/11  Dr. Lale T. Ergene Function  to calculate factorial ! Function to calculate factorials recursively ! input : integer n => 0 ! output : n ! function factorial (n) result (fact) integer, intent (in) : : n integer : : fact, i fact = 1 do i = 2, n fact = fact * i end do end function factorial 66
Background image of page 6
 3/14/11  Dr. Lale T. Ergene Recursive function  to calculate factorial ! Function to calculate factorials recursively ! Input : integer n => 0 ! Output : n ! recursive function
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 03/13/2011.

Page1 / 23

WEEK8 - Week8 ClicktoeditMastersubtitlestyle...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online