Can't understand how this program returns value as 5, instead of 6. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Can't understand how this program returns value as 5, instead of 6.

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

17th Jun 2017, 8:11 AM
The KMM
The KMM - avatar
8 Answers
+ 2
addOneTo function make a local copy from num and increase that copied value but not return it. If you want to retzrn 6, change its return type int amd assign the function to x, or set num static and dont pass it as function argument.
17th Jun 2017, 8:26 AM
Szabó Gábor
Szabó Gábor - avatar
+ 2
I still don't understand this.. Can someone explain it further to me? How do I make the return value 6 instead of 5? Maybe I can see the difference much better.
24th Jun 2017, 5:56 AM
Ellis Mendoza
Ellis Mendoza - avatar
+ 2
write return num and change the type int instead void. In the main assign the nww value zo x , x = addOneTo(x);
24th Jun 2017, 8:21 AM
Szabó Gábor
Szabó Gábor - avatar
+ 1
This is so simple, when we call the function (addOneTo) we pass 'x' as an argument, meaning that we pass the copy of the value 'x'. So whatever changes made to the copy it will not be reflected in the original variable 'x'. Here 'x' and 'num' are both different variables. Secondly there is no RETURN STATEMENT in the (addOneTo) function, to return the result. Thirdly we are printing the value of 'x' not the value of 'num' That is why the answer is 5.
28th Jun 2017, 9:41 AM
Farrukh Ijaz
Farrukh Ijaz - avatar
+ 1
For me, the explanation by @Farrukh Ijaz was the easiest. From what I understand: int x = 5: Here, we define the value of 5 to the integer variable "x". So now, we have the value of "5" stored in variable "x". addOneTo(x) : Here, we have a function called "addOneTo". We pass our "x" as a value to it. Similarly, we could write: addOneTo(5). This does not do anything else, just tells a function named "addOneTo" that the number it should work with is contained within a variable called "x". This does not alter the number stored in the variable "x", however. For example, if I tell you that I just made $100, you can go on and do something with that information, but I still have $100 - me telling you that does not change the amount of dollars I have. "You(MoneyIMade)" = "addOneTo(x)" = "You($100)" = "addOneTo(5)". I hope this makes sense. System.out.println(x): This just prints out the value of "x". The value of "x" still stays "5", since we didn't do anything to change it's value, we just told someone else what that value is. static void addOneTo(int num) { num = num + 1 : Here, our "addOneTo" actually executes. The function knows that when you mention it by name in code, it can accept a single parameter of type integer. In our case, that integer is the "x". So judging by this code, "addOneTo" gets the value of "x", which is 5, adds 1 to it, and then results the final value - 6 - to the variable named "num". That's all good, but there is nothing in our code that outputs the variable "num", we only have something that outputs the variable "x". And since we didn't do anything to change the "x", then it still stays 5. I hope it's somewhat clearer. IMO SL didn't do too good of a job explaining this here.
25th Jul 2017, 5:57 AM
Jonathan Boyko
+ 1
Jonathan Boyko thank you this was the best explanation! i still have a question what if we used pointer. like we give to x the adress of 5 the result will always be the same?
11th Nov 2018, 8:49 AM
Liza BOUMALI
Liza BOUMALI - avatar
0
I too have difficulty understanding why this is not 6. The explanations so far have not been easy to understand.
26th Jun 2017, 12:10 AM
Ste Ross
Ste Ross - avatar
0
Because it's only passing a value, not returning it. That's why the code is : //Change void into int(void means don't return anything, so we need to change it to int(return type) static void addOneTo(int num) { //Not retur num = num + 1; //If you want to increment the value, you need to type return num; }
27th Jun 2017, 1:22 AM
Sinar Nadhif Ilyasa
Sinar Nadhif Ilyasa - avatar