Lab 7 Nov 15,16 (2PM), 17

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 45

Recursion

Recursion is the process


of repeating items in a
self-similar way.

[1]https://willrosenbaum.com/teaching/2021s-cosc-112/notes/recursive-image/
Recursion
• Recursion is a method of solving a computational problem
where the solution depends on solutions to smaller instances
of the same problem.
• A programming technique where a function calls itself!
A Function A Recursive Function

Output Output

There will be infinite Hello messages !


Function
(Local Namespace/Scope)

Main
(Global Namespace/Scope)
Function
(Local Namespace/Scope)

Main
(Global Namespace/Scope)

There is a function called sayHello and it starts from the line 1!


Function
(Local Namespace/Scope)

Main
(Global Namespace/Scope)

sayHello line 1
Function
(Local Namespace/Scope)

Main
(Global Namespace/Scope)

sayHello line 1

Is there such a function


called sayHello() ?
Function
(Local Namespace/Scope)

Main
(Global Namespace/Scope)

sayHello line 1
Function
(Local Namespace/Scope)

Main
(Global Namespace/Scope)

sayHello line 1
Function
(Local Namespace/Scope)

Main
(Global Namespace/Scope)

sayHello line 1
Function
(Local Namespace/Scope)

Main
(Global Namespace/Scope)

sayHello line 1
Function
(Local Namespace/Scope)

Main
(Global Namespace/Scope)

sayHello line 1
Function
(Local Namespace/Scope)

Main
(Global Namespace/Scope)

sayHello line 1
Function
(Local Namespace/Scope)

Main
(Global Namespace/Scope)

sayHello line 1
Function
(Local Namespace/Scope)

Main
(Global Namespace/Scope)

sayHello line 1
Function
(Local Namespace/Scope)

Main
(Global Namespace/Scope)

sayHello line 1
• In programming, goal is to NOT have infinite recursion

• We must have 1 or more base cases that are easy to solve

NOT GOOD !
Sum of Integers From 1 to N
sumNumber(6)
sumNumber(6) sumNumber(5)
sumNumber(6) sumNumber(5) sumNumber(4)
sumNumber(6) sumNumber(5) sumNumber(4) sumNumber(3)
sumNumber(6) sumNumber(5) sumNumber(4) sumNumber(3) sumNumber(2)
Recursive Step Base Step

1
sumNumber(6) sumNumber(5) sumNumber(4) sumNumber(3) sumNumber(2) sumNumber(1)

BASE
3

sumNumber(6) sumNumber(5) sumNumber(4) sumNumber(3) sumNumber(2)


6

sumNumber(6) sumNumber(5) sumNumber(4) sumNumber(3)


10

sumNumber(6) sumNumber(5) sumNumber(4)


15

sumNumber(6) sumNumber(5)
21 sumNumber(6)
QUESTION

Calculate the factorial of a given number by using recursion


Recursive Step Base Step

return -> 5 * factorial(4)

factorial(5)
return -> 4 * factorial(3)

factorial(5) factorial(4)
return -> 3 * factorial(2)

factorial(5) factorial(4) factorial(3)


return -> 2 * factorial(1)

factorial(5) factorial(4) factorial(3) factorial(2)


RETURN !

factorial(5) factorial(4) factorial(3) factorial(2) factorial(1)

BASE
return -> 2 * factorial(1)
1

factorial(5) factorial(4) factorial(3) factorial(2) factorial(1)


return -> 3 * factorial(2)
2

factorial(5) factorial(4) factorial(3) factorial(2)


return -> 4 * factorial(3)

factorial(5) factorial(4) factorial(3)


return -> 5 * factorial(4)

24

factorial(5) factorial(4)
120 factorial(5)
QUESTION

a) Find Nth Fibonacci number by using recursion


b) Print first N Fibonacci numbers
Hint : Just call the function in option a in a for loop

Input : 9 Output : 21
Input : 9 Output : 0 1 1 2 3 5 8 13 21
f(5)

f(3) f(4)

f(1) f(2) f(2) f(3)

f(1) f(2)
References
• MIT OPENCOURSEWARE- Introduction to Computer Science and
Programming In Python, Fall 2016
• Gökçe Uludoğan- CMPE 150, Fall 2021
• Mehmet Turan – CMPE 150, Fall 2023

You might also like