Recursive Function

Recursion (computer science), a procedure or subroutine, implemented in a programming language, whose implementation references itself” Wikipedia. In this case, Recursive Function is a function that call or reference it self to do some repetitive actions. Different with iteration function that use loop, recursion function dont use loop to do repetitive function. Therefore, Recursive Function may be called several times while the iteration function only called once.

This is simle example of recursive factorial function (example using javascript).

function Factorial_Recursive(n){
	if (n <= 1) return 1;
	else
	return n * Factorial_Recursive(n - 1);//this call itself
}

In this case, Factorial_Recursive(n) function will call itself and be executed several times if you declare the ‘n’ variable at least 2.

Other more simple example function using php.

function Simple_Recursive($n){
	if($n!=0){
		echo $n.' ';
		$n--;
		Simple_Recursive($n);
	}
}

Simple_Recursive(10);

The above function will give output “10 9 8 7 6 5 4 3 2 1”.

To put it simple, recursive funcition is a function that call it sefl to do repetitive action.

http://septiadi.com/2011/06/13/recursive-function/

Advertisements

2 responses to “Recursive Function

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: