I had an exam today and there was a question which I don’t really remember the exact code.
But what I do remember that inside the method exampleMethod(int num) there was a line calling the method itself inside the method.
I want to know can a method be called inside it self? because it was a multiple choice question and they wanted from us to find the output.
I hope my question is clear.
Thanks SOF 🙂
Sure it can. When you do that it’s called recursion. Be careful that you have an exit condition or you will get a stack overflow.
for example
EDIT — here is the same example but with a different break-out, to compliment @Ted’s comment
however I prefer to always be as explicit as possible, so I would explicitly break out as in the first example, if possible.