Skip to main content

Methods

Why do we use arguments and return type in a method?

Using arguments and return values in a method allows us to create a piece of code that can be used independently, like a helpful tool.

Let's understand this with an example of a method we write to add two numbers:

int addNumbers(int n1, int n2) {
    int result = n1 + n2;
    return result;
}

Anyone can use this method without knowing how it works internally. They just need to know two things:

  1. Arguments: What values to provide when using the method (in this case, two numbers).
  2. Return Value: What the method will give back as a result (in this case, the sum of the two numbers).

In fact, we have been using quite a few methods from the very beginning of this course without knowing how they work. Think about the System.out.println() method we've been using from the beginning of this course. We don't know exactly how it works inside, but we can use it because we know if we give it something to print, it will display that on the output.

These methods, like System.out.println(), nextInt(), nextDouble(), etc., are called standard library methods. Their definitions are already built into the Java Library, and we can use them by just calling them.

So, when you create a method to solve a problem, always consider two questions:

  1. What arguments will the method take?
  2. What value should the method return?

This way, your method becomes a handy tool that others (or even yourself) can use without worrying about the internal details.

Common Mistakes for Beginners

Mismatched Return Type

Consider this example:

class Main {
 
    int multiplyNumbers(double num1, double num2) {
        double product = num1 * num2;
        return product;
    }
 
    public static void main(String[] args) {
 
        Main obj = new Main();
 
        int result = obj.multiplyNumbers(3.5, 4.6);
        System.out.println(result);
    }
}

Imagine you have a method to calculate the product of two numbers, and it returns a decimal (like 8.4). But, oops! You try to catch it in a whole number (int) bucket. It's like trying to fit a watermelon into a coffee mug – it just won't work.

The program will throw a fit because it expected a decimal, not a whole number.

Not Using the Return Value Properly:

Now, think of a method that figures out the area of a rectangle. It calculates it perfectly (let's say 24 square units), but you forget to write it down or show it off. It's like baking a delicious cake and leaving it in the kitchen without telling anyone.

class Main {
    
    double getArea(double length, double width) {
        double area = length * width;
        return area;
    }
    
    public static void main(String[] args) {
        
        Main obj = new Main();
        
        obj.getArea(6, 4);
    }
}

The method calculates, but since we don't capture or showcase the result, it's like the area never existed.

Note: If a method hands you something valuable, make sure to keep it safe or share it somehow.

Incorrect Number of Arguments:

Think of a method like ordering a pizza. If you call the pizza place and say, "Give me a large pizza," it's cool. But if you say, "Give me a large pizza with pepperoni and mushrooms," and they only hear "large pizza," you might end up with a plain cheese pizza. The toppings matter!

import java.util.Scanner;
 
class Main {
 
    double getCost(double baseCost, double toppingCost) {
        double totalCost = baseCost + toppingCost;
        return totalCost;
    }
 
    public static void main(String[] args) {
 
        Main obj = new Main();
 
        double result = obj.getCost(15.0);
        System.out.println(result);
    }
}

Here, Java will complain because the getCost() method wants two things, but we only provided one during the order. It's like trying to pay for a large pizza when you ordered a large pizza with extra cheese.

Understanding these coding scenarios helps prevent headaches and keeps your code running smoothly!