-1
public class Main
{
    static int plusMethodInt(int x, int y){
        System.out.println(x+y);
    }
    static double plusMethodDouble(double x, double y){
        System.out.println(x+y);
    }
    public static void main(String[] args){
        plusMethodInt(8, 5);
        plusMethodDouble(4.3, 5.6);
    }
}

Why am I not able to run this snippet without utilizing return statement?

Priya
  • 11
  • 4
  • Please edit your post to format your code. It's *really* hard to read without any indentation. But fundamentally, you've declared that your methods return values - but you haven't returned any. Either change the return type to `void` (given that you're not using the return values anyway) or use `return` statements to return values. – Jon Skeet Apr 16 '22 at 09:08
  • Yeah it worked now, Thankss – Priya Apr 16 '22 at 09:15

1 Answers1

1

Your method declaration is not correct. If you do not want to return any value from the method, then it's return type should be void and not int and double ( as you have declared in the method).

That is why your compiler is throwing error. Rectify it to this :

public class Main {
    static void plusMethodInt(int x, int y) {
        System.out.println(x + y);
    }
    static void plusMethodDouble(double x, double y) {
        System.out.println(x + y);
    }
    public static void main(String[] args) {
        plusMethodInt(8, 5);
        plusMethodDouble(4.3, 5.6);
    }
}
Zabon
  • 241
  • 2
  • 18
Gurkirat Singh Guliani
  • 1,009
  • 1
  • 4
  • 19