Introduction to Java: Passing Parameters

When I first started learning Java, I was fairly perplexed by the parentheses found after every method.  Sometimes they were empty, and sometimes they had values in them, and I didn’t understand why.  Plus, in the declaration of a method, there was a variable mentioned before the method name, and I didn’t know why that was there.  I’ve found it’s easiest to think about parameters and methods in terms of input and output.   Here’s an example:

output Square (input) {

}

The green output describes what kind of result you are going to return from the method.  The green input describes what kind of input I put into the method.  This method is going to take a number, square it, and return the result of the squaring.  I would like the input number to be an integer, so I will declare that here:

output Square (int input) {

}

The word “input” is what I’m calling the variable that I’m going to pass into the method.  Because I know that if I square an integer, I will get an integer as a result, I’m going to declare that the output will be an integer:

int Square (int input) {

}

Note that I don’t need to create a variable name for the output when I’m declaring the method.  Now let’s add in the steps for the method:

int Square (int input) {

int output = input * input;
return output;

}

So what this function does is take the variable called input, squares it, sets it to equal an int variable called output, and then returns the output.

It’s also possible to have more than one input parameter:

String Square (int input, String name) {

int output = input * input;
String result = “Hi ” + name + “!  Your result is ” + ” output;
return result;

}

This is a little more confusing, so I will explain it in more detail.  The first line of the method is taking the input variable and squaring it, and setting it to a variable called output, just as it did before.  But in the second line we’re creating a String.  The characters in between the quote marks are entered into the string.  These are concatenated (added) with our int output and the String name that we entered in the parameters, using plus signs to add the characters:

Hi ” + name + “!  Your result is ” + ” output

So if I called the function like this:

Square(2, Kristin);

The result String would be set to:

Hi Kristin!  Your result is 4

Then if my main program wanted to output the result, I would use the instruction:

System.out.println (result);

And my result would be printed to the console.

Now, what about methods that don’t pass in any parameters?  It would seem as if parameters would be needed to run a method, but they aren’t.  Here’s an example:

void MyParameterlessMethod () {

System.out.println (“I can print something out without even passing in a parameter!”);

}

All this method is going to do is run the print instruction inside, which doesn’t need any parameter. You may have noticed the void before my method name.  This says that the method isn’t going to return anything to the main program.

Is it possible to have a method that doesn’t pass in any parameters, but still returns something? Sure!

int MyParameterlessMethod () {

int result = 4;
return result;

}

This method assigns the number 4 to the variable called result, and then returns it to the main program.

In my next post, I will discuss setting up Eclipse and writing a simple program.