Professional Documents
Culture Documents
Dimana,
n>0
Fungsi tersebut menunjukkan sifat rekursi berupa pemanggilan terhadap fungsi factorial – n
berulang kali.
Faktorial (1){
1
if (2 > 1){…}
else return 1;}
Manajemen Informatika Politeknik Manufaktur Astra 2017
Konsep Last in First Out Pada Recursion
I want to compute sumInts(2, 5)
The computation yielded 0, therefore I shall return 5 = 5 + 0.
for this, I need to compute sumInts(3, 5)
The computation yielded 5, therefore I shall return 9 = 4 + 5.
and add 2 to the result. The computation yielded 9, therefore I shall return 12 = 3 + 9.
I want to compute sumInts(3, 5) The computation yielded 12, therefore I shall return 14 = 2 + 12.
for this, I need to compute sumInts(4, 5)
and add 3 to the result.
func sumInts(a: Int, b: Int) -> Int {
I want to compute sumInts(4, 5)
if (a > b) {
for this, I need to compute sumInts(5, 5) return 0 }
and add 4 to the result. else {
return a + sumInts(a: a + 1,b:
I want to compute sumInts(5, 5) b) }
for this, I need to compute sumInts(6, 5) }
Manajemen
since Informatika
6 > 5, this is zero. Politeknik Manufaktur Astra 2017
Konsep Last in First Out Pada Recursion
Should I use
iteration or
recursion?
Time/space efficiency.
Dale, Nell B., et al. 2001. Object Oriented Data Structured Using
Java. Sudbury: Jones and Bartlett Publishers, Inc.
Hubbard, John R. 2007. Data Structure With Java. New York:
McGraw-Hill Companies, Inc.
Lafore, R. 1999. Sams Teach Yourself Data Structures and
Algorithms in 24 Hours. Indianapolis: Sams Publishing.