#Recursive Function, multiplication.
1 messages · Page 1 of 1 (latest)
can anyone explain line 18
I did it alone after messing too much with it, got a little help from chatGPT
but it doesn't make sense still
x + function(x, y-1)
Try writing it yourself, recursion is something you should grasp.
I already memorized it, so I would end up writing the same
is there any other way to write it
how are we summing x with (x, y-1)
The function is calling itself with different values. Place some breakpoints and follow the flow.
Shouldn't it be 10?
yes im trying to repeat the function till it gets to 0
am i doing it right
You start with x = 5, y = 10.
ik but I chose my own numbers now
5 + ( 5 +(0))
yep
wait sorry if thats a dumb question but does (5,1) mean anything
like do we calculate anything here
or we just assigning numbers
arguments to the recursive call
you are doing x=5, y=1 when you reach the
return x + multiplyTest(x,y-1)
are you off by one?
we added the new 5 to the previous 5
Have you run the code?
yes
what does it print
So the third time multiplyTest gets called, y is zero.
You can stick a print message in there to help see it at each stage.
wait so it doesn't end here?
It's never 10
that would be 15
public class Main {
public static void main(String[] args) {
int x = 5;
int y = 2;
System.out.println(multiplyTest(x,y));
}
public static int multiplyTest(int x, int y){
if (x == 0 || y == 0){
return 0;
}
else
return x + multiplyTest(x, y - 1);
}
}
It's a completely different 'x' each time you enter the method.
It's a completely different 'x' each time you enter the method.
yea
WAIT im lost sorry
is there something wrong with me, or the code
or both
the code gives me correct answers
OH I KNOW WHY bro
System.out.println( "x="+x+", y="+y);
cuz the code stops, when y or x are 0.
paste that before the if(x==0... part.
And so you can see as it enters the third time, y=0, and the if statement catches that and returns.....0
So 5 + 5 + 0
is 10
because 5 x 0 = 0?
I presented it as 5 + ( 5 +(0))
i see ur point
While yes, you can multiple numbers, that is not the reason. zero is just a stopping point.
6x3 = 18, but it's also 6 + 6 + 6
yea
that sounds more expert...
omg, are we doing your fucking homework now?
Struggle a bit then seek help.
ok!
@junior bramble that was a bit rude
@placid bay its totally okay to ask for help on ur homework. we are here to help and explain. just remember that we will try to guide u such that u can solve it urself and not "give you the answer". thats all there is to it. so u have to bring a bit of time to listen, understand and then do it urself with our guidance 👍
Yes I know that you don’t give answers, I’ve got a lot of help from this server thanks to you and your members :)
What part was rude?
omg, are we doing your fucking homework now?
Struggle a bit then seek help.
perhaps it got lost in translation, in case it isnt ur mothertongue. but it reads kinda hostile and unfriendly overall
You're magically in this thread because you read all the threads?
i do read all messages on the server, yes. but for this particular one, several users messaged me to have a look 😛
Ok, just clarifying that you know why you're here. What you are missing is the context that was deleted by OP. He tried to hit me up with his next homework question. I made the comment in jest, it was received as such. He then deleted it.
The struggle part is exactly as it sounds, I'm asking him to try it himself first. There is no issue here.