Method Recursion

What is recursion?

Recursion is when something calls itself. When a method calls itself, it’s referred to as a recursive method.

Syntax

public static double AMethod()
{
  return AMethod(); // Method calls itself
  // Note: This example would crash a program because it would never end
}

Example: VerifyWord

Suppose we’re designing a game where user input determines the direction we move, either upwards or downwards. Here is an example that uses recursion, where we repeatedly ask the user to make sure that they’ve input the corect direction.

Get hands-on with 1200+ tech skills courses.