0

Could someone please explain in very simple terms why the code below outputs 35 and not 999 when the array is in essence passed by reference and temp[0] is 999 and passed into someFunction() as num1.

public class TestB {

public static void main(String[] args)
{
    int[] num1 = {35};
    someFunction(num1, 3);     
    System.out.println(num1[0]);  //prints 35
}

public static void someFunction(int[] num1, int count){

    if (count!= 0){
        int[] temp = {999};
        someFunction(temp, count-1);
    }
}

}

mjwills
  • 23,389
  • 6
  • 40
  • 63
Tortoise
  • 65
  • 6
  • Because you reassigned the `temp` variable, not the `temp[0]` element of the array. It's not pass-by-reference, it's pass-by-sharing. – Bergi Jun 30 '19 at 13:29
  • It's Java. As I trace through the algorithm, on the second call, num1[0] is 999 and continues to be until I return back to main. – Tortoise Jun 30 '19 at 13:36
  • Java is **never** pass by reference. If you pass `num1` to the `someFunction` method – call it 'method' rather than 'function' – then the value of the reference is copied to `someFunctions` first parameter (which appears to have the same name). – MC Emperor Jun 30 '19 at 14:00

1 Answers1

1

someFunction does not read or write to the num1 parameter. Thus, someFunction by definition can't impact the num1 array passed in to it from main.

This would be the case, regardless of whether array parameters in your language of choice were passed by reference or by value.

As I trace through the algorithm, on the second call, num1[0] is 999 and continues to be until I return back to main.

I suspect you are getting confused by the fact that your variable name in main and your parameter name in someFunction are the same. You are seeing num1 as set to a particular value - but that isn't main's num1 - that is someFunction's num1. To make that clearer when debugging, change the name of one of them (either the variable name in main or the parameter name in someFunction) to bob instead.

In terms of the outcome you are trying to achieve I suspect, that perhaps instead of:

public static void someFunction(int[] num1, int count){

    if (count!= 0){
        int[] temp = {999};
        someFunction(temp, count-1);
    }
}

you may have meant to write:

public static void someFunction(int[] num1, int count){

    if (count!= 0){
        num1[0] = 999;
        someFunction(num1, count-1);
    }
}

Note in particular that (with my suggested change) someFunction will be writing to the num1 parameter (the parameter passed in, not a new temp array) - so the array in main will reflect that change.

mjwills
  • 23,389
  • 6
  • 40
  • 63
  • I see what you're saying. Can you explain simply why when you trace the call num1[0] becomes 999? Are the parameters local to each call? – Tortoise Jun 30 '19 at 13:50
  • The `num1` is `someFunction`'s `num1`. Change the name of one of the `num1`s to `bob` and you'll spot your mistake very quickly. – mjwills Jun 30 '19 at 13:52
  • 1
    Ah apologies I think you answered this point above. Thank you! – Tortoise Jun 30 '19 at 13:52