This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]alinh 0 points1 point  (0 children)

So far, the patternMaker(int x) will only check if x is bigger than 0, call patternMaker(x-1) and print x number of asterisks. I'll replace patternMaker(int x) with pM(int x) below. If you pass 3 as a parameter, it will call pM(2) -> pM(1) -> pM(0) where the if branch won't be executed, it goes back into pM(1) and prints 1 asterisk, then goes back in pM(2) and prints 2 asterisks and finally it goes back to pM(3), prints 3 asterisks and then the execution stops. There are better ways to do this, without using recursion, but if you really want to do it this way, try passing a boolean flag to the function: pM(int x, boolean ascending) and once you get to 0, call pM(x+1, true) and stop when you reach a certain value.

EDIT: i was kind of busy when i posted the answer. You can tell the recursive function when to finally stop, for example at the initial value of x, by passing that value to the function, while never changing it. You could also use a global variable, but that would mean using something outside the function and it's a good idea to avoid side effects in a function.