Page 227 - CA_Blue( J )_Class10
P. 227

▪   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
                                        }



                 Method Name
                 Every method should be provided with a name, so that it can be called from same class or different classes. Method
                 name should be a valid identifier. The method name should be meaningful and related to the task done by it. For
                 example,

                                                  public int sum (int i, int j)


                 Parameter List
                 One or more parameters are passed to a method within the pair of parentheses while defining the method. There are
                 data types along with variable names are passed to the method as parameters. These parameters are separated by
                 comma. For example,

                                                  public int sum (int i, int j)

                 The values of these parameters are passed at the time of calling the method in the same order in the parameters are
                 defined in the definition of the method. In some methods, there may not be any parameter which may be known as
                 empty parameter list. For example,

                                                          public int sum ()


                 Body of the Method
                 The body of a method is defined by the curly brackets. Inside the curly brackets, a set of statements are defined to
                 perform the desired task. When a method is called, the statements written inside the curly brackets are executed. For
                 example,

                                     public int sum (int i, int j)
                                      {
                                         int s=a + b;
                                                                                      Body of the Method
                                         return s;
                                      }


                                                                                                                       225
                                                                                                 User-defined Methods   225
   222   223   224   225   226   227   228   229   230   231   232