#Recursive Function, multiplication.

1 messages · Page 1 of 1 (latest)

nocturne oxideBOT
#

<@&987246399047479336> please have a look, thanks.

placid bay
#

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)

atomic latch
#

Try writing it yourself, recursion is something you should grasp.

placid bay
#

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)

atomic latch
#

The function is calling itself with different values. Place some breakpoints and follow the flow.

placid bay
#

wait lemme actually use it theorically

#

x = 5, y= 2

#

5 + 2 = 7

junior bramble
#

Shouldn't it be 10?

placid bay
#

7 + 1

#

8

placid bay
#

am i doing it right

atomic latch
#

You start with x = 5, y = 10.

placid bay
junior bramble
#

5 + ( 5 +(0))

placid bay
#

my brain went error

junior bramble
#

It just adds x, y times.

#

which is x*y

placid bay
#

hold on can we move step by step? lets say x is 5, y is 2

#

5 + (5,1)?

junior bramble
#

yep

placid bay
#

wait sorry if thats a dumb question but does (5,1) mean anything

#

like do we calculate anything here

#

or we just assigning numbers

junior bramble
#

arguments to the recursive call

#

you are doing x=5, y=1 when you reach the
return x + multiplyTest(x,y-1)

placid bay
#

so now

#

5 + (10,0)

#

omg

#

yes?

junior bramble
#

are you off by one?

placid bay
#

we added the new 5 to the previous 5

junior bramble
#

Have you run the code?

placid bay
#

yes

junior bramble
#

what does it print

placid bay
#

10

#

if x = 10, y= 2

junior bramble
#

So the third time multiplyTest gets called, y is zero.

placid bay
#

yea

#

u're talkin about (10,0)

junior bramble
#

You can stick a print message in there to help see it at each stage.

placid bay
junior bramble
#

It's never 10

placid bay
#

then how did my code print 10

#

?

junior bramble
#

that would be 15

placid bay
#

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);

}
}

junior bramble
#

It's a completely different 'x' each time you enter the method.

placid bay
#

but how did the code give me 10?

#

which is correct

#

5 x 2

junior bramble
#

It's a completely different 'x' each time you enter the method.

placid bay
#

yea

junior bramble
#

no where are you assigning an x = somevalue.

#

so how could it be 10

placid bay
#

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

junior bramble
#

System.out.println( "x="+x+", y="+y);

placid bay
#

cuz the code stops, when y or x are 0.

junior bramble
#

paste that before the if(x==0... part.

placid bay
#

oh ok sec

junior bramble
#

put that print on line 13

#

then rerun

placid bay
#

it prints this

junior bramble
#

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

placid bay
#

because 5 x 0 = 0?

junior bramble
#

I presented it as 5 + ( 5 +(0))

placid bay
#

i see ur point

junior bramble
placid bay
#

yes

#

its the base case

#

yea?

#

i feel smart calling it a base case

junior bramble
#

6x3 = 18, but it's also 6 + 6 + 6

placid bay
junior bramble
#

I call it the 'bailout condition'

#

or base case.

placid bay
junior bramble
#

omg, are we doing your fucking homework now?

placid bay
#

LMAO

#

ok fine thanks

junior bramble
#

Struggle a bit then seek help.

placid bay
#

it's not a homework

#

im studying for a final exam

placid bay
simple canopy
#

@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 👍

placid bay
junior bramble
simple canopy
#

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

junior bramble
#

You're magically in this thread because you read all the threads?

simple canopy
junior bramble
#

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.