Programming Java help!!!!!!!!!

Pumpkin Ninja

Kage in the Making 👑
Legendary
Joined
Oct 31, 2012
Messages
15,554
Reaction score
1,857
Does anyone know how recursion works? If so can you help me? And before you ask I've looked everywhere but could not find anything useful...
 

Hexuze

Legendary Shinobi 🐸
Supreme
Joined
May 19, 2011
Messages
20,359
Reaction score
1,533


It's excellent for math help. I'm pretty sure they can help with programming in general.
 

abcplay57

Member
Joined
Aug 4, 2012
Messages
73
Reaction score
14
Recursion works like a loop. It "loops" by calling the method within itself. But to ensure that it doesn't go on forever, you need to include a base case that will end the recursion sequence. Usually in programming classes it has a format like (for example)...

private static void example(int counter)
{

//base case
if(counter == 10)
{
System.out.println("Recursion done");
}
else //recursive step
{
counter++;
example(counter);
}
}

After that make sure you call it first in the main method.

public static void main(String[] args)
{
example(0);
}

This is a reaaaally simple example.
 

waTiem

Member
Joined
Dec 9, 2012
Messages
9
Reaction score
2
sure.

All function are called with an activation record which is a data structure that contains all of the variables that are passed to the function and is placed on the program stack. A recursive function is a function that calls itself; each time it calls itself, a new activation record is created. The trick is to establish a stopping condition or otherwise call itself with different parameters so that eventually you reach the stopping condition.

As an example, here's a java function that prints "Sakura Sucks" 2^k times (call it with k =8 for 256 times).


static void SS(int k)
{
if(k == 0) System.out.println("Sakura Sucks");
else
{
SS(k - 1);
SS(k - 1);
}
}


It's the power of computers!
 

6ari8

Jōnin Strategist 🧠
Veteran
Joined
Jul 6, 2011
Messages
2,209
Reaction score
209
The main thing is to understand the Base Case and the Recursive case. Everything else is fairly simple.

I think the best example you can find for recursion is the factorial function. This example can help you:
 

Wabbit

Banned
Legendary
Joined
Nov 15, 2011
Messages
11,335
Reaction score
797
Recursion is the process of repeating itself several times until the condition is true.
A function calling it self is a recursive function.Idk java.
It would be like this in general for functions in C or C++
Code:
function()
            {
               statement;
                function(); //calling the same function.
               }
 

Pesh

Sannin of the Scrolls 📜
Elite
Joined
Oct 21, 2008
Messages
5,501
Reaction score
435
For me, this is one of the best tutorials on Java recursion.
It's very well explained, uses programming terminology and has some great exercises and examples.
 
Top