Java recursive functions explained // Using recursion to sum an array of numbers 2


Java is a very powerful object oriented language. If you search for recursion in wikipedia you will find this definition Recursion, in mathematics and computer science, is a method of defining functions in which the function being defined is applied within its own definition.” This means, that the function will call itself again and again until it gets the correct answer.

There are many pages where you can find great explanations to recursion theory. Most of them use the example of the Towers of Hanoi:

What I’m going to show you is an easy way to sum arrays of numbers. This example lets you see how easy can recursion be and how useful it is.


Leave a comment

Your email address will not be published. Required fields are marked *

2 thoughts on “Java recursive functions explained // Using recursion to sum an array of numbers

  • Peter Veentjer

    The problem with recursive calls in Java is that Java doesn’t do much ‘tail call optimization’. The consequence is that for each call you need another stackframe, and this will lead to problems (stack overflow and performance).

    Functional programming languages like Haskell or Erlang do provide tail call optimization and in these languages a recursive call is just as expensive as an iteration.