r/learnjava • • Feb 08 '21

Help me understand recursion, please!

public static long method(long a, long b) {
    if (b == 0) {
        return b;
    }
    return a + method(a, b - 1);
}

So this apparently resolves to essentially a * b. I really don't understand this. The base case only ever returns b when b is zero, so in my flawed logic it's always a * 0 = 0.

51 Upvotes

15 comments sorted by

View all comments

14

u/mikezyisra Feb 08 '21

You’ve got plenty of helpers here, I just want to thank you for having the courage to speak up about needing help with new concepts, it takes some guts. Good luck in the future

3

u/Apostle_1882 Feb 10 '21

I think easily my biggest flaw is not asking for help. When I have most of the time I get the help I need and a positive response, I just worry about asking a stupid question and being annoying. I should ignore that, within reason of course.