Logout

Methods Vis-a-vis Parameters & Return Values

As seen in the previous methods notes, methods sometimes take in parameters, which affect the way the method executes. And methods sometimes return something to where they were called, and sometimes they are void of a return value. So that means there are four combinations of the way methods can be stuctured, in terms of whether or not they take in parameter and whether or not they return values. The four possibilities are:

Type-A: Returns nothing, and takes no parameters
Type-B: Returns nothing, and takes in parameters.
Type-C: Returns something, and takes in no parameters.
Type-D: Returns something, and takes in parameters.

Here are some very simple examples of each:

Type-A: Returns nothing, and takes no parameters

public void aMethod(){ System.out.println("Hello World.");
}

Type-B: Returns nothing, and takes in parameters.

public void aMethodWithParameters(String a, String b){
        System.out.println(a + " " + b);
}
 

Type-C: Returns something, and takes in no parameters.

public String aMethodWithReturn(){
        return "Hello World";
}

Type-D: Returns something, and takes in parameters.

public String aMethodWithParametersAndReturn(String a){
        return("Hello " + a);
}

Note that the above methods would each result in the output of: Hello World if called as follows:


public static void main(String [] args){ aMethod();
aMethodWithParameters("Hello", "World"); System.out.println(aMethodWithReturn()); System.out.println(aMethodWithParametersAndReturn("World"));
}

Output:

Hello World.
Hello World.
Hello World.
Hello World.

So it's neat to see that the same output could be produced with the four different ways of combining parameters taken in or not and return value used or not, but appreciate that there are times where it makes sense to use each of the four approaches. The methods above are trivial, and are used here just to demonstrate the combinations you'll encounter, and also to reinforce that the use of return values and the use of parameters are independent of each other.