Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Recursion | Recursion and Lambda Functions
course content

Course Content

Python Functions Tutorial

RecursionRecursion

Recursion in Python is when a function calls itself. It's a useful technique for solving complex problems by breaking them down into smaller parts. The key elements of recursion are the base case (the termination condition) and the recursive case (where the function calls itself).

Let's go step by step through how this recursive program works:

  1. Base Case Check: The function checks if times is greater than 0. In our case, times is 3, satisfying the condition;
  2. Print Message: The function prints the message "Hello, Recursion!";
  3. Recursive Call: The function calls itself with the same message and times - 1 (which is 2 in this case);
  4. New Call: In the new call, the same process repeats: base case check, printing the message, and another recursive call with times - 1 (now 1);
  5. Final Call: The final call happens for times = 1, the last message is printed, and immediately the recursion exits as the base case condition is met;
  6. Completion of Calls: Each recursive call completes, and control is passed back to the previous call;
  7. Program Termination: The program returns to the initial call, and the program execution completes.

The result will be the printing of the message "Hello, Recursion!" three times.

Task

Your task is to fill in the blanks (___) in the function that will calculate the sum of the digits of a positive number using recursion:

  1. Base case: if the number is less than 10 the function must return this number as the result.
  2. Recursive case: firstly, we extract the last digit from the number using the modulus operator (%) and then add this number to the sum of the remaining digits. The remaining digits can be found using the integer division operator (//).

Everything was clear?

Section 5. Chapter 1
toggle bottom row
course content

Course Content

Python Functions Tutorial

RecursionRecursion

Recursion in Python is when a function calls itself. It's a useful technique for solving complex problems by breaking them down into smaller parts. The key elements of recursion are the base case (the termination condition) and the recursive case (where the function calls itself).

Let's go step by step through how this recursive program works:

  1. Base Case Check: The function checks if times is greater than 0. In our case, times is 3, satisfying the condition;
  2. Print Message: The function prints the message "Hello, Recursion!";
  3. Recursive Call: The function calls itself with the same message and times - 1 (which is 2 in this case);
  4. New Call: In the new call, the same process repeats: base case check, printing the message, and another recursive call with times - 1 (now 1);
  5. Final Call: The final call happens for times = 1, the last message is printed, and immediately the recursion exits as the base case condition is met;
  6. Completion of Calls: Each recursive call completes, and control is passed back to the previous call;
  7. Program Termination: The program returns to the initial call, and the program execution completes.

The result will be the printing of the message "Hello, Recursion!" three times.

Task

Your task is to fill in the blanks (___) in the function that will calculate the sum of the digits of a positive number using recursion:

  1. Base case: if the number is less than 10 the function must return this number as the result.
  2. Recursive case: firstly, we extract the last digit from the number using the modulus operator (%) and then add this number to the sum of the remaining digits. The remaining digits can be found using the integer division operator (//).

Everything was clear?

Section 5. Chapter 1
toggle bottom row
some-alt