Passing a parameter versus returning it from function

This is more about best practices and your own method to program. I would say if you know this is going to be a one value return type function like:

function IsThisNumberAPrimeNumber{ }

Then you know that this is only going to ever return a boolean. I usually use functions as helper programs and not as large sub procedures. I also apply naming conventions that help dictate what I expect the sub\function will return. Examples:

GetUserDetailsRecords GetUsersEmailAddress IsEmailRegistered

If you look at those 3 names, you can tell the first is going to give you some list or class of multiple user detail records, the second will give you a string value of a email and the third will likely give you a boolean value. If you change the name, you change the meaning, so I would say consider this in addition.


Returning a value from the function is generally a cleaner way of writing code. Passing a value and modifying it is more C/C++ style due to the nature of creating and destroying pointers.

Developers generally don't expect that their values will be modified by passing it through a function, unless the function explicitly states it modifies the value (and we often skim documentation anyway).

There are exceptions though.

Consider the example of Collections.sort, which does actually do an in place sort of a list. Imagine a list of 1 million items and you are sorting that. Maybe you don't want to create a second list that has another 1 million entries (even though these entries are pointing back to the original).

It is also good practice to favor having immutable objects. Immutable objects cause far fewer problems in most aspects of development (such as threading). So by returning a new object, you are not forcing the parameter to be mutable.

The important part is to be clear about your intentions in the methods. My recommendation is to avoid modifying the parameter when possible since it not the most typical behavior in Java.


You should return it. The second example you provided is the way to go.

First of all, its more clear. When other people read your code, there's no gotcha that they might not notice that the parameter is being modified as output. You can try to name the variables, but when it comes to code readability, its preferable.

The BIG reason why you should return it rather than pass it, is with immutable objects. Your example, the List, is mutable, so it works okay. But if you were to try to use a String that way, it would not work.

As strings are immutable, if you pass a string in as a parameter, and then the function were to say:

public void fun(String result){
    result = "new string";
}

The value of result that you passed in would not be altered. Instead, the local scope variable 'result' now points to a new string inside of fun, but the result in your calling method still points to the original string.

If you called:

String test = "test";
fun(test);
System.out.println(test);

It will print: "test", not "new string"!

So definitely, it is superior to return. :)