From the course: Functional Programming with Java

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Recursion

Recursion - Java Tutorial

From the course: Functional Programming with Java

Start my 1-month free trial

Recursion

- [Instructor] The next advanced concept, we're going to look at is called recursion. Now, this is a concept that you've probably heard before but maybe you've never seen it in action. Recursion is simply when a function calls itself. And while doing this can very easily lead to an infinite loop, if we're not careful. It can also be used to solve certain problems that aren't easy to solve using functional programming. So, let's take a look at a very simple example using recursion. What we're going to do, is create a function that behaves like a for loop, without actually using a for loop. So, before we get started, one kind of goofy thing about recursion in Java is that we can't really do it by defining functions with lambda syntax. Well, it is possible, but in my opinion, it's a little clumsy. So for now, we'll just use a regular static method for this example. So what we're going to do is we're going to call this function countdown. So we'll say, static void, countdown. And what…

Contents