Arguments, parameters, return values

Return values

A method that is a function takes an input and returns an output. The method definition must specify the type of whatever is returned. The type identifier appears right before the method identifier in the definition. For example, here is a method that returns a random integer between 0 and 99 (don't worry about how it actually works). Notice that the body of the method contains the reserved word return right before the expression that calculates the value that's returned.

public int getRandomInt100() {
  return (int)(Math.random() * 100);
}

Arguments

Some methods take inputs, data that they need to do what they do. When such a method is called, these inputs are passed as the arguments to the method, surrounded by parentheses and separated by commas. Note that a method that takes no arguments must still be followed by parentheses when it is called.

Parameters

To specify which arguments a method takes, the method definition include a parameter for each of the arguments. Each parameter must be given a type and an identifier. The parameters are enclosed by parentheses and separated by commas. For example, here is the definition of a method that takes two integer arguments and returns the result of subtracting the first one from the square of the second one.

public int subSquare(int x, int y) {
  return y * y - x;
}

Home

Calendar

Coursework & grading

Assignments

Lecture notes

Other resources


IU home

IU CS home

Contact instructor