- #1

- 11

- 0

can someone explain java recursion to me?

Last edited:

You should upgrade or use an alternative browser.

- Java
- Thread starter physicsfun
- Start date

- #1

- 11

- 0

can someone explain java recursion to me?

Last edited:

- #2

- 494

- 0

Basically, you have functional recursion and type recursion. Type recursion would be like a linked list. Functional recursion would be like a function calling itself. For instance...

Code:

```
class LinkedNode
{
public int data;
public LinkedNode next;
}
```

LinkedNode is a recursively defined type:

LinkedNode : null | int & LinkedNode.

For functions...

Code:

```
int factorial(int n)
{
if(n<=0)
return 1;
else
return n * factorial(n-1);
}
```

This is saying that factorial is 1 if n<=0, and n * factorial(n-1) otherwise (n > 0).

Basically, recursive definitions need a base case (null, n<=0) and a recursive part (some new information PLUS some (usually smaller) instance of the very same definition).

- #3

- 261

- 2

Share: