Home Alone – Kevin is not here!

First, let’s watch this short video clip from the movie Home Alone 2.

This video clip highlights a typical case of recursion.

Recursive Functions

A recursive function is a function which calls itself.
To make sure that a recursive function does not loop indefinitely it needs to check if a condition is met: If it is, it should stop calling itself.

In this challenge we are writing a recursive function that asks the person standing next to us to “pass this bag to Kevin”. Each person in the queue forwards this message by calling the same function. However if we reach the end of the queue or if we reach Kevin then we stop asking. So in this case we have two conditions that can stop the recursion: we reach the end of the queue/list or we reach Kevin.

Give this to Kevin: Recursive Function

For more examples based on recursive functions, check this post.

Did you like this challenge?

Click on a star to rate it!

Average rating 5 / 5. Vote count: 1

No votes so far! Be the first to rate this post.

As you found this challenge interesting...

Follow us on social media!