-3
public class homework
{
    public static void intPow(int a, int b)
    {
        Math.pow(a,b);
    }


    public static void main(String args[])
    {
        intPow();
    }
}   

I'm trying to learn how to create a method, but I keep getting 10 ; expected errors. I know this code isn't correct, but I can't seem to find how to create a method correctly. In this case I'm trying to create a method that returns a^b.

user2770254
  • 89
  • 1
  • 7

6 Answers6

8

You need to pass two int parameters into intPow():

public static void main(String args[])
    {
        int a = 2;
        int b = 5;
        intPow(a, b); //32
    }

Furthermore, you should probably return an int from intPow() so you can play with it later:

public static int intPow(int a, int b) { 
    return Math.pow(a, b);
}

Then in main():

public static void main(String args[])
    {
        int a = 2;
        int b = 5;
        int power = intPow(a, b); //32
        System.out.println(power);
    }
Bucket
  • 7,415
  • 9
  • 35
  • 45
  • The result is not 32 since the return type of intPow() is void. – David Conrad Sep 25 '13 at 19:20
  • Thanks, but I'm just curious, why do you have to specify the variables in both main and the method? Since it says (a, b) in the method, why doesn't that carry to intPow();? – user2770254 Sep 25 '13 at 19:21
  • 1
    Think of those as just placeholders - whatever you pass into `intPow()` we will just call `a` and `b` **inside** the function. – Bucket Sep 25 '13 at 19:22
  • The parameters in the method are called formal parameters. They define what the values will be called inside the method. The parameters that are passed to the method are called actual parameters. They determine what the actual values will be when the method runs. – David Conrad Sep 25 '13 at 19:23
  • 1
    @user2770254 the method declares that it needs 2 parameters to work. If you call it and do not pass the variables as declared, it will generate a compilation error. – Luiggi Mendoza Sep 25 '13 at 19:23
  • I'm digressing a bit, but why did you remove the "static" from the method? Not criticizing, just curious. – user2770254 Sep 25 '13 at 19:26
  • There is no reason for it to be static - there is nothing in `intPow()` that can only have one instance of its scoped object. See http://stackoverflow.com/questions/413898/what-does-the-static-keyword-do-in-a-class – Bucket Sep 25 '13 at 19:27
  • It's generating an error since I changed double to int. Why is this? – user2770254 Sep 25 '13 at 19:28
  • First of all, make sure your return types match their assignments. You cannot call `double foo = bar()` if `bar` returns an `int`. You must either make `bar` return a double or declare `foo` as an `int`. – Bucket Sep 25 '13 at 19:31
  • "non-static method cannot be referenced from a static context" – user2770254 Sep 25 '13 at 19:33
  • Oi, of course, careless overlook on my part. Go ahead and switch it back to static, my mistake! :) (See edit) – Bucket Sep 25 '13 at 19:35
3

pass two int values in intPow();

intPow(5,5);

And anyways the value would not be printed.

You need to use System.out.println() to print it.

JNL
  • 4,683
  • 18
  • 29
3

Change

intPow();

to

intPow(2,3); // or any number

hrv
  • 925
  • 6
  • 13
2

You declare intPow as a function that takes two parameters. But when you call it from main, you dont pass any. To fix this, change this line in main -

intPow();

to

intPow(1, 2);//or whatever other numbers you want. 
David says Reinstate Monica
  • 19,209
  • 22
  • 79
  • 122
2
    public class homework
    {
        public static int intPow(int a, int b)
        {
            return Math.pow(a,b);
        }


        public static void main(String args[])
        {
            int a = 3;
            int b = 4;
            int result = intPow(a, b);
            System.out.println(result);
        }
    }   
2

If the goal is to create a method that returns a^b, the method should return a value. You probly need to convert to int though, because Math.pow works with doubles.

public static int intPow(int a, int b) {
    return (int) Math.pow(a,b);
}

then call it using two parameters for a and b:

int result = intPow( 2, 3 );
Jeroen Vuurens
  • 1,171
  • 1
  • 9
  • 10