Recursion

What is recursion?

Recursion is the method of calling the same function inside the function.

This is the modern approach for the loops in programming languages. So I thought of write about recursion before loops.

Capture

More Zoomed image is given below.

Capture1

Here I have created a function named “find” which returns an integer. It will also take 2 integers as the input. The basic aim of this function is to ¬†find the random number that generated by using a recursive function.

The same function will be called in the else part by reducing the value of x (which is equivalent to r initially) and increasing the value of y by one by one.

When x equals to zero the function will stop.

How recursion happens?

At the beginning the if condition fails (only if the random number is greater than 0). Then it will run the else part. It says that return but again go through the same function by reducing the value of x and increasing the y. Here the counter is y. When x goes to zero y will be equal to the initial x.

Finally the return statement in if part will return the value to else part and the return statement in else part will return y to the main method.

Special Key Words

Random creates a random integer.

NextInt creates that random integer to be in between 0 and 10.

 

Hope you found something new about recursion and i will discuss further about recursion in future posts.

Thank You.

 

Advertisements

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