Control FlowCore· 35 min read

Methods (Parameters & Return)

A method is a named block of code you can reuse — it can take inputs and give back a result.

What you will learn

  • Write your own method
  • Pass parameters into a method
  • Return a value from a method

Code you can reuse

A method is a named chunk of code you can run whenever you like, just by calling its name. You have already used methods (println) and written one (main). Now you will create your own.

Methods help you avoid repeating yourself: write the steps once, call them many times.

A method called three times with different inputs
public class Greeter {

    // a method that takes a name and prints a greeting
    static void greet(String name) {
        System.out.println("Hello, " + name + "!");
    }

    public static void main(String[] args) {
        greet("Asha");
        greet("Ravi");
        greet("Mia");
    }
}

Note: Output: Hello, Asha! Hello, Ravi! Hello, Mia! We wrote greet once, then called it three times. The value in the brackets (the argument) fills the parameter name each time.

Parameters and return values

  • Parameters are the inputs a method accepts, listed in its brackets (like String name).
  • Return value is the answer a method gives back. The return type comes before the name.
  • void means the method returns nothing — it just does something (like printing).

Here is a method that returns a value instead of printing:

A method that returns an int
public class Calc {

    // takes two ints, returns their sum
    static int add(int a, int b) {
        return a + b;
    }

    public static void main(String[] args) {
        int total = add(5, 3);
        System.out.println("Total is " + total);
        System.out.println("Another: " + add(100, 50));
    }
}

Note: Output: Total is 8 Another: 150 add returns a + b. The returned value can be saved in a variable (total) or used directly inside println. Because it returns an int, the return type is int (not void).

You may wonder about the word static in front of these methods. For now it simply lets main call the method directly, without making an object first. When you reach classes and objects in the next unit, you will write methods without static that belong to a specific object — but the idea of parameters and return values stays exactly the same.

Tip: Good methods do one clear job and have a descriptive name: calculateTotal, isEven, greet. If a method is getting long, that is a hint to split it into smaller methods.

Q. What does a return type of void mean?

Answer: void means no value is returned. Methods that calculate an answer use a real type like int or String and use return.

✍️ Practice

  1. Write a method square(int n) that returns n * n, and print square(6).
  2. Write a void method line() that prints a row of dashes, and call it twice.

🏠 Homework

  1. Write a method isEven(int n) that returns a boolean (true if n is even), then test it on a few numbers.
Want to learn this with a mentor?

CodingClave runs guided, project-based training (28-day, 45-day & 6-month batches).

Explore Training →