Page 117 - 2617_JSSPS_C-7
P. 117

Return Type and Return Statement
                 A method is used only when it is called by another method. So, after completing the assigned task,
                 the  method returns control to the portion from where it is called. While returning the control, a
                 method may or may not return a value along with it. For this purpose, we require return type and
                 return statement.

                    The return Statement: The return statement is required if the function is returning some value.
                   The returned value may be of any data type, even non-primitive  data types. Following  are the
                   characteristics of return statement:
                    •  It is used to return a value from the method to the calling program.

                    •  It is always the last line of a method.
                    •   By default, it always returns a single value of one data type only. If we want to return more than
                      one value, we have to create a non-primitive data type and return. For example,


                                                    int[] sum ()
                                                    {
                                                        int i;
                                                        int ar[] = {1,2,3,4,5};
                                                        for (i=0; i<5; i++)
                                                        {
                                                            ar[i] = ar[i]+2;
                                                        }
                                                        return ar;
                                                    }


                    •  There may be more than one termination point, but only one point will be executed. For example,


                                                String show()
                                                {
                                                    if(result>0)
                                                        return "Positive";
                                                    else
                                                        return "Negative";
                                                }


                    Return Type: If the function is returning a value, then the data type of the value is written before the
                   function name. The different data types used as return type are byte, short, int, long, float, double,
                   char or Boolean. We can also use String if we want to return a word or a sentence. Other than these,
                   all non-primitive data types such as array, class, etc. can be used as return type. If a function is not
                   returning any value, then the keyword "void" is used.  For example,


                                      public int sum (int i, int j)
                                       {                                              Return Type
                                          int s=a + b;
                                          return s;                                  Return Statement
                                       }






                                                                                               Java Programming    115
   112   113   114   115   116   117   118   119   120   121   122