9 Replies - 990 Views - Last Post: 13 July 2010 - 07:18 AM Rate Topic: -----

#1 Zinny  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 13-July 10

Missing Return Statement

Posted 13 July 2010 - 06:35 AM

I tried running a program i wrote is a program tha calculates two numbers . And is telling me a missing return statement. what do i do
Is This A Good Question/Topic? 0
  • +

Replies To: Missing Return Statement

#2 Krishs  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 23
  • View blog
  • Posts: 105
  • Joined: 06-April 10

Re: Missing Return Statement

Posted 13 July 2010 - 06:36 AM

View PostZinny, on 13 July 2010 - 06:05 PM, said:

I tried running a program i wrote is a program tha calculates two numbers . And is telling me a missing return statement. what do i do

did you forget to add return statement to method...
Please post code to see what you have done!!

This post has been edited by Krishs: 13 July 2010 - 06:38 AM

Was This Post Helpful? 1
  • +
  • -

#3 eZACKe  Icon User is offline

  • Garbage Collector

Reputation: 120
  • View blog
  • Posts: 1,278
  • Joined: 01-June 09

Re: Missing Return Statement

Posted 13 July 2010 - 06:51 AM

Keep in mind that if your method type is not void you have to be returning something.

For example, if your method is "public double getDouble()" you need to have something along the lines of "return doubleVariable".


Also, another reason this could be happening is if you have a method that looks something like this:

public double getDouble()
{
     if(x > 10)
     {
         double doubleVariable = 2.0;
         return doubleVariable;
     }
}



The above example will return an error because a double will only be returned if x > 10.

Hope this helps, post your code and I can assist you further.

This post has been edited by eZACKe: 13 July 2010 - 06:53 AM

Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10438
  • View blog
  • Posts: 38,651
  • Joined: 27-December 08

Re: Missing Return Statement

Posted 13 July 2010 - 06:54 AM

Title renamed to be more descriptive.

We can't help you if you don't post your code as well as where you are getting your errors.
Was This Post Helpful? 0
  • +
  • -

#5 Zinny  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 13-July 10

Re: Missing Return Statement

Posted 13 July 2010 - 06:54 AM

class Calculation
{
  int x;
  int y; 
  int z;
  public Calculation(int x, int y)
    {
      this.x=x;
      this.y=y;
      }
   public int add ( ) 
     { 
        z=x+y; 
       System.out.println("x+y == " + x  +"  "+ y + " ==" + z);
      }
   public static void main(String[ ] args)
      {
        Calculation c = new Calculation (20 ,10);
        c .add( );
      }
}


Edited by Dogstopper, please :code:

This post has been edited by Dogstopper: 13 July 2010 - 07:13 AM

Was This Post Helpful? 0
  • +
  • -

#6 eZACKe  Icon User is offline

  • Garbage Collector

Reputation: 120
  • View blog
  • Posts: 1,278
  • Joined: 01-June 09

Re: Missing Return Statement

Posted 13 July 2010 - 06:57 AM

It's just as I said above, your method is type int but you are not returning anything. If you just want to print and not return an actual value, use type void.

So you want:
public void add()


Was This Post Helpful? 1
  • +
  • -

#7 Zinny  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 13-July 10

Re: Missing Return Statement

Posted 13 July 2010 - 07:06 AM

is still not finding my call
Was This Post Helpful? 0
  • +
  • -

#8 techfreak :)  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 13-July 10

Re: Missing Return Statement

Posted 13 July 2010 - 07:11 AM

View PostZinny, on 13 July 2010 - 06:06 AM, said:

is still not finding my call

As they said above, you have to use void if function doesn't return anything.
class Calculation
{
    int x;
    int y; 
    int z;
    public Calculation(int x, int y)
    {
        this.x=x;
        this.y=y;
    }
    public void add ( ) // LOOK HERE
    { 
        z=x+y; 
        System.out.println("x+y == " + x +" "+ y + " ==" + z);
    }
    public static void main(String[ ] args)
    {
        Calculation c = new Calculation (20 ,10);
        c .add( );
    }
}


Edited by Dogstopper, we'd prefer that you have code tags and a comment than without code tags. Thank you.

This post has been edited by Dogstopper: 13 July 2010 - 07:15 AM

Was This Post Helpful? 0
  • +
  • -

#9 eZACKe  Icon User is offline

  • Garbage Collector

Reputation: 120
  • View blog
  • Posts: 1,278
  • Joined: 01-June 09

Re: Missing Return Statement

Posted 13 July 2010 - 07:13 AM

Once you make the method void, it does work.

I just ran it here's the result:

"x+y == 20 10 ==30"
Was This Post Helpful? 0
  • +
  • -

#10 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,025
  • Joined: 15-July 08

Re: Missing Return Statement

Posted 13 July 2010 - 07:18 AM

If you'd rather that the add method add and NOT print, then you can do something more like this, using the return statement.


class Calculation
{
    int x;
    int y; 
    int z;
    public Calculation(int x, int y)
    {
        this.x=x;
        this.y=y;
    }
    public int add ( ) // specify it returns an int
    { 
        z=x+y; // Add the numbers
        return z; // This "Gives" z back to the method that called add().
    }
    public static void main(String[ ] args)
    {
        Calculation c = new Calculation (20 ,10);
        System.out.println(c .add( )); // This calls add() and prints the result
    }
}


Often, this method is liked better since now you can use add() elsewhere since it returns a value. (You can do more things using value than just printing)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1