This site requires JavaScript, please enable it in your browser!
Greenfoot back
drewleighton
drewleighton wrote ...

2013/1/17

Programming error

drewleighton drewleighton

2013/1/17

#
why would the programing come back with "missing return statement"?
Duta Duta

2013/1/17

#
If you get that error, then you've attempted to create a method that returns a value ...but you've never returned a value. For example:
public int add(int a, int b)
{
    int c = a + b;
}
^This would give an error, as you need to return a value. The method should be this:
public int add(int a, int b)
{
    int c = a + b;
    return c;
}
If you are creating a method with "void" return type, then it doesn't return a value, and so you do not need a return statement. You can put return statements in a method with "void" return type - this will just have the effect of exiting the method. For example:
public void move(int distance, int time)
{
    if(time == 0)
    {
        return;
    }
    int speed = distance/time;
    this.x += speed;
    this.y += speed;
}
If you post the method that is giving you the error, I can give more specific help.
You need to login to post a reply.