Week 4 — What is meant by recursion?

Question of the Week #4
What is meant by recursion?
4 Replies
Eric McIntyre
Eric McIntyre2y ago
The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function.
Eric McIntyre
Eric McIntyre2y ago
Submission from sahilasopa#2787
No description
Eric McIntyre
Eric McIntyre2y ago
recursion - function/method that calls itself immediately, or with another method/function(s). for example:
class Main {
/**
* @returns number's length (num) + another number (counter), or just the other number (counter), if the number (num) is 0.
*/
public static int rec1(int num, int counter) {
if (num == 0) return counter;
return rec1(num / 10, counter + 1);
}

// like rec1, but with 2 methods (rec2, rec3).
public static int rec2(int num, int counter) {
if (num == 0) return counter;
return rec3(num / 10, counter + 1);
}

public static int rec3(int num, int counter) {
if (num == 0) return counter;
return rec2(num / 10, counter + 1);
}
}
class Main {
/**
* @returns number's length (num) + another number (counter), or just the other number (counter), if the number (num) is 0.
*/
public static int rec1(int num, int counter) {
if (num == 0) return counter;
return rec1(num / 10, counter + 1);
}

// like rec1, but with 2 methods (rec2, rec3).
public static int rec2(int num, int counter) {
if (num == 0) return counter;
return rec3(num / 10, counter + 1);
}

public static int rec3(int num, int counter) {
if (num == 0) return counter;
return rec2(num / 10, counter + 1);
}
}
this can be repeated with up to infinity methods/functions.
Submission from Avasay_Sayava#8277
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server