Privacy note: We capture your IP address to provide customised and focused services. See our Privacy Policy.

AP Computer Science A: Deep Dive and FRQ Mastery — Alpha School

Lesson 3: Methods, Parameters and Return Values

A method is a named block of code that performs a task, and methods are how Java programs are organized into manageable pieces. Instead of writing one long main method, you break the work into methods with clear names — calculateAverage, isPassing, printReport — each doing one job. Methods make programs easier to read, easier to test, and easier to reuse, and the free-response section of the exam is largely about writing them correctly, so this lesson's ideas are worth mastering thoroughly.

A method is defined by its signature: its return type, its name, and its parameter list. The return type says what kind of value the method gives back, or void if it gives back nothing. The parameters are the inputs the method needs, each declared with a type and a name, and they behave like local variables that are initialized with the values passed by the caller, called arguments. When the method finishes, a return statement sends a value back to the caller, and the method's local variables disappear. Keeping parameters, arguments, and return values straight is essential: parameters are the placeholders in the definition, arguments are the actual values in the call.

Java passes arguments by value, which means the method receives a copy of each argument. For primitives, the copy is the value itself, so changing a parameter inside the method never affects the caller's variable. For objects, the copy is the reference, so the method cannot make the caller's variable point to a different object, but it can change the object that the reference points to. This distinction explains many exam questions: a method that reassigns its object parameter has no effect on the caller, while a method that mutates the object through the parameter does.

Methods can call other methods, including themselves — the subject of the recursion unit — and a well-chosen method can hide complexity behind a simple name. When you write a method, start from its contract: what it needs, what it returns, and what it promises. Then write the body to fulfil that contract and nothing more. In free-response answers, check your signature first: the return type, the name, and the parameter types must match the question exactly, because a method with the wrong signature is a different method, and the marker cannot give credit for code that would not compile as asked.

Methods come in two broad kinds, and knowing which one you need is the first design decision. A value-returning method computes something and gives it back, such as a method that returns the larger of two numbers, and its result is used in an expression by the caller. A void method performs an action, such as printing a report or updating an object's fields, and returns nothing. A void method can still change things: it can modify the object it was called on, or an object passed to it, and those changes persist after the call returns. When you design a method, decide first whether its job is to compute a value or to perform an action, because that decision fixes its return type and shapes its whole body.

A worked example

Worked example: the method below takes two int scores and returns their average as a double. Notice the signature, the use of 2.0 to avoid integer division, and the return statement that ends the method with a value of the declared type.

public double average(int a, int b)
{
    return (a + b) / 2.0;
}

// a call: double result = average(80, 90);  // result is 85.0

The caller passes 80 and 90 as arguments, the parameters a and b receive copies, and the return statement computes (80 + 90) / 2.0, which is 85.0, and sends it back. If the method had divided by 2 instead of 2.0, the result would have been the int 85 converted to 85.0 — the same answer here, but wrong for odd sums, where the decimal part would be lost. Trace a method call by writing down the parameter values, following the body line by line, and noting what the return statement sends back.

Key Terms

Method
A named block of code that performs a task, defined by a signature and a body.
Signature
A method's return type, name, and parameter list.
Parameter
A variable in a method definition that receives a value from the caller.
Argument
The actual value passed to a method when it is called.
Return value
The value a method sends back to its caller with a return statement.
Pass by value
Java's rule that a method receives a copy of each argument.

Discussion Questions

  1. What is the difference between a parameter and an argument?
  2. A method changes its int parameter. Why doesn't the caller's variable change?
  3. Why must a method's return type match the value it returns?

Practice Questions

  1. Write the signature of a method named max that takes two ints and returns an int.
  2. What does a return type of void mean?
  3. A method has parameter String name. What is passed when it is called with "Maya"?
  4. A method is supposed to return a double but its body has no return statement. What happens?
  5. Inside a method, a parameter int x is set to 0. Does the caller's variable change?
Show answers
  1. public int max(int a, int b)
  2. The method returns no value.
  3. A copy of the reference to the String object containing "Maya".
  4. It is a compile-time error: a non-void method must return a value of its declared type on every path.
  5. No, because Java passes primitives by value; the method works on a copy.
Keep reading to complete this lesson