You are on page 1of 3

4/9/2012

Jurusan Teknik Informatika Universitas Widyatama

Overview
Tujuan Instruksional Review Fungsi & Prosedur Introduction of Recursion Definition Recursion Method Contoh Kasus Recursion vs Iteration Kasus Tugas Presentasi

Rekursif
Pertemuan : 6-7 Dosen Pembina : Danang Junaedi

Tujuan Instruksional
Menjelaskan pengertian dan manfaat rekursif, serta cara penulisannya dalam program Menjelaskan kelebihan dan kekurangan rekursif dalam pemrograman Menjelaskan penggunaan rekursif dalam program Menggunakan rekursif dalam program

Review Fungsi dan Prosedur


Fungsi tipe_fungsi nama_Fungsi(argumen1, argumen2,....) definisi Fungsi { xxxx xxxx tubuh Fungsi } contoh : double Absolut(double X) { if (X,0) X=-X; return(X); } Prosedur void nama_Prosedur(argumen1, argumen2,....) definisi Prosedur { xxxx xxxx tubuh Prosedur } contoh : void Tampil(char Nama[15], int Kali) { int I; for(I=0;I<Kali;I++) printf(Nama); }

4/9/2012

Introduction of Recursion
So far, we have seen methods that call other methods.

Definition
A recursive function is a function that calls itself either directly or indirectly through another function. The function launches (calls) a fresh copy of itself to work on the smaller problem this is referred to as recursive call and is also called as recursion step. The recursion step often includes the keyword return The function recognizes the base case and returns a result to the previous copy of the function and a sequence of returns ensues all the way up the line until the original function call eventually returns the final result to main

For example, the main() method calls the square() method.


main() square()
Recursive Method:

A recursive method is a method that calls itself.


compute()

Recursion Method
The important thing to remember when creating a recursive function is to give an 'end-condition'. We don't want the function to keep calling itself forever, now, do we? Somehow, it should know when to stop. There are many ways of doing this. One of the simplest is by means of an 'if condition' statement A recursive method generally has two parts.

Example (1)
Fungsi Faktorial Secara Iterasi C++
int Fakt(int n) { Fak=1; if (n > 1) { for(i=1;i<=n;i++) { Fak=Fak*I;} } return Fak; } int Fakt(int n) { if (n == 1 || n == 0) { return 1;} else {return n * Fakt(n-1);} }

A termination part that stops the recursion.

This is called the base case. The base case should have a simple or trivial solution.
One or more recursive calls. atau n! = n * (n - 1) * (n - 2) * ... * 1 Secara Rekursif

This is called the recursive case. The recursive case calls the same method but with simpler or smaller arguments.

atau n! = n * (n - 1)! dan 0! = 1

4/9/2012

Example (2)
Ilustration
5! 5 * 4! 4 * 3! 3 * 2! 2 * 1! 1
a. Process of recursive calls

Recursion vs Iteration

5!
5! = 5 * 24 = 120 returned

5 * 4!
4! = 4 * 6 = 24 returned

4 * 3!
3! = 3 * 2 = 6 returned

3 * 2!
2! = 2 * 1 = 2 returned

2 * 1!
1 returned

1
b. Values returned from each recursive calls

Iteration Uses repetition structures (for, while or dowhile) Repetition through explicitly use of repetition structure Terminates when loop-continuation condition fails Controls repetition by using a counter Recursion Uses selection structures (if, ifelse or switch) Repetition through repeated method calls Terminates when base case is satisfied Controls repetition by dividing problem into simpler one More overhead than iteration More memory intensive than iteration Can also be solved iteratively Often can be implemented with only a few lines of code

Studi Kasus
Towers of Hanoi game
(Game invented by French mathematician Edouard Lucas in 1883)

Untuk bahan renungan bersama


1. Jadilah Jagung, Jangan Jambu Monyet. Jagung membungkus bijinya yang banyak, sedangkan jambu monyet memamerkan bijinya yang cuma satusatunya. Artinya : Jangan suka pamer 2. Jadilah Duren, jangan kedondong. Walaupun luarnya penuh kulit yang tajam, tetapi dalamnya lembut dan manis. hmmmm, beda dengan kedondong, luarnya mulus, rasanya agak asem dan di dalamnya ada biji yang berduri. Artinya : Don't Judge a Book by The Cover.. jangan menilai orang dari Luarnya saja. 3. Jadilah bengkoang. Walaupun hidup dalam kompos sampah, tetapi umbinya isinya putih bersih. Artinya : Jagalah hati jangan kau nodai. 4. Jadilah Tandan Pete, bukan Tandan Rambutan. Tandan pete membagi makanan sama rata ke biji petenya, semua seimbang, tidak seperti rambutan.. ada yang kecil ada yang gede. Artinya : Selalu adil dalam bersikap. 6. Jadilah Cabe. Makin tua makin pedas. Artinya : Makin tua makin bijaksana. 7. Jadilah Buah Manggis. Bisa ditebak isinya dari pantat buahnya. Artinya : Jangan Munafik 8. Jadilah Buah Nangka. Selain buahnya, nangka memberi getah kepada penjual atau yg memakannya. Artinya : Berikan kesan kepada semua orang (tentunya yg baik).

Rules for the Towers of Hanoi game 1. Move one disk at a time. Each disk you move must be a topmost disk. 2. No disk may rest on top of a disk smaller than itself. 3. You can store disks on the second pole temporarily, as long as you observe the previous two rules. Fibonacci (F(n)=F(n-1) + F(n-2) + )

Binary Search secara rekursif

You might also like