Results 1 to 10 of 10

Thread: about 'return'.

  1. #1
    helloworld is offline Member
    Join Date
    Sep 2008
    Posts
    11
    Rep Power
    0

    Default about 'return'.

    // Exercise Using If to return a grade
    //
    class Grade {

    public static char getGrade (int score) {
    char grade;
    if (score >= 80) {
    grade = 'A';
    } else if (score >= 70) {
    grade = 'B';
    } else if (score >= 60) {
    grade = 'C';
    } else if (score >= 50) {
    grade = 'D';
    } else {
    grade = 'F';
    }
    return grade;
    }

    // Startup method
    public static void main(String[] args) {
    System.out.println(getGrade(75));
    System.out.println(getGrade(60));
    System.out.println(getGrade(49));
    System.out.println(getGrade(-3));
    System.out.println(getGrade(110));
    }
    }


    Hey guys, I don't know what is the use of the keyword 'return', and when do you use it. I've seen 'if else' codes with and without the 'return true return false' . And in the above example, it says 'return' (something else that isn't true or false). I'm confused as to what is really means when you use it.

    For another example,

    public boolean isFit() {
    if (calBMI() > 23)
    return false;
    else
    return true;
    }

    In this case, is it correct to say that I'm using 'return false' or 'true' to continue running the rest of the codes below it? or what does it mean?

    And for the first example, what does the program do when you tell it to 'return grade'?

    Thank you guys.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    return is a Java keyword used to exist/finish the execution of a method. Before exist, method can return(give back) data to another method.

    According to your first method, use a conditional statements if-else and assign the value to variable grade. Then give that value to another method, main method.

    In the second case, you return a true/false.

    Key thing you have to keep in mind is, once a data/value is return the method execution is exist, rest of the code is not executed.

  3. #3
    helloworld is offline Member
    Join Date
    Sep 2008
    Posts
    11
    Rep Power
    0

    Default

    So is it correct to say that 'return' is used, in order to allow the 'original' values to be used by other methods within or outside a class?

    And when exactly do you need to 'return' something?


    Thanks!
    Last edited by helloworld; 11-25-2008 at 08:52 AM.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  5. #5
    helloworld is offline Member
    Join Date
    Sep 2008
    Posts
    11
    Rep Power
    0

    Default

    Pardon my weird 'analogy' or way of saying this, so

    when used in a method say,


    public int getEmpSal()
    {
    return empSal;
    }




    the keyword 'return' can literally be used to return something on the ms dos screen when the method is called,

    and can also be used to 'return' to the original value within the program itself?

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    You cannot return any value to the console. What you can do is, can return a value to another method and print to the console using println(). Same thing you have done is first two methods you have post here.

  7. #7
    Deepa is offline Member
    Join Date
    Nov 2008
    Posts
    85
    Rep Power
    0

    Default Re:

    Hi,
    We must return any value when we use method.it returns true or false.

  8. #8
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Smile demonstrate use of return

    Java Code:
    //
    class RoadGrader 
    {
        // Store grades
        String[] grades;
        
        // Constructor.
        RoadGrader(String[] args) 
        {
            grades = args;
        }
        //
        public boolean getGrades()
        {
            try
            {
                //  examines all the grades, if supplied on command line.
                for(String nextGrade : grades)
                {
                    int oneGrade = Integer.parseInt( nextGrade );//
                    switch(oneGrade)
                    {
                        case 90 :  {System.out.println('A');break;}
                        case 80 :  {System.out.println('B');break;}
                        case 70 :  {System.out.println('C');break;}
                        case 60 :  {System.out.println('D');break;}
                        case 50 :  {System.out.println('E');break;}
                        case 40 :  {System.out.println('F');break;}
                        case 30 :  {System.out.println('G');break;}
                        case 20 :  {System.out.println('H');break;}
                        case 10 :  {System.out.println('I');break;}
                    }
                }
                return true;
            }
            catch(Throwable t)
            {
                return false;
            }
        }
    
        // Startup method
        public static void main(String[] args) {
            RoadGrader rg = new RoadGrader( args ); 
            if(rg.getGrades())
            {
                System.out.println("Done"));
            }
            else
            {
                System.out.println("An error is encountered");
            }
        }
    }
    Code is not intended to be available for direct use in your assignment, it is to demonstrate use of return.

    try and catch are something you have not studied yet, but notice we return false if needful to catch the condition of grade not being entered correctly.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  9. #9
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default Not necessarly...

    Deepa wrote:
    We must return any value when we use method.it returns true or false.
    I would like to clarify somethings in the statements above:
    • You don't have to return data/info ("any value") from a method if it's not necessary. What if the method's job was to print something on the screen, like a warning. In this case, there is not need to return anything from the method.
    • Methods can return any value that the method is coded to return. For example, public String askName() returns a string.
    • "return" is considered a branching statement... here's more info:
    • Branching Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Deepa View Post
    Hi,
    We must return any value when we use method.it returns true or false.
    If you want comment on something, please try to make it more clearly. Seems most of the members mess with your replay.

Similar Threads

  1. there is no return statement
    By gabriel in forum New To Java
    Replies: 17
    Last Post: 12-03-2008, 05:55 PM
  2. Using int/int, 7/5 would return 1
    By zoe in forum New To Java
    Replies: 2
    Last Post: 12-02-2008, 12:25 PM
  3. return out of scope?
    By another_steve in forum New To Java
    Replies: 5
    Last Post: 01-28-2008, 10:34 PM
  4. if..else..return
    By mqdias in forum New To Java
    Replies: 1
    Last Post: 08-10-2007, 05:20 PM
  5. The return type
    By Marcus in forum New To Java
    Replies: 1
    Last Post: 07-05-2007, 07:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •