0

why isnt the output 6? or would it have print num and not x?

public class MyClass { public static void main(String[ ] args) { int x = 5; addOneTo(x); System.out.println(x); } static void addOneTo(int num) { num = num + 1; } }

16th Sep 2019, 3:28 AM
Andrew DeRiemacker
Andrew DeRiemacker - avatar
8 Réponses
+ 1
You need to ude refernce, wtite the function like this: (int& num)
16th Sep 2019, 3:36 AM
KfirWe
KfirWe - avatar
+ 1
~ swim ~ Ohh i didn't know that😐
16th Sep 2019, 4:21 AM
KfirWe
KfirWe - avatar
+ 1
thanks anyways. to both of you.
16th Sep 2019, 4:24 AM
Andrew DeRiemacker
Andrew DeRiemacker - avatar
+ 1
You have to use pass by reference method
16th Sep 2019, 1:47 PM
Bullet Raja
Bullet Raja - avatar
+ 1
When we use to pass addres of variable x than any changes occured in x will also change the value of x in main function tooo
16th Sep 2019, 1:48 PM
Bullet Raja
Bullet Raja - avatar
0
Oh it doesn't return anything from the method. Is that right?
16th Sep 2019, 3:31 AM
Andrew DeRiemacker
Andrew DeRiemacker - avatar
0
ok thanks
16th Sep 2019, 3:44 AM
Andrew DeRiemacker
Andrew DeRiemacker - avatar
0
thanks. i appreciate your answer
16th Sep 2019, 1:51 PM
Andrew DeRiemacker
Andrew DeRiemacker - avatar