+ 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; } }
8 odpowiedzi
+ 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.
+ 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.
+ 2
write return num and change the type int instead void.
In the main assign the nww value zo x , x = addOneTo(x);
+ 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.
+ 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.
+ 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?
0
I too have difficulty understanding why this is not 6. The explanations so far have not been easy to understand.
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;
}