Page 219 - Cs_withBlue_J_C11_Flipbook
P. 219

The general syntax of declaring a method:
                    <access specifier> <return-type> <name-of-method> <parameter-list>
                    {
                    Job done by the method;
                    }

                    For example,
                    public int twice (int n)
                    {
                        int tw=n*2;
                        return tw;
                    }
                 Explanation:
                 Here, “twice” is the name of the method. The word “int” stands for integer and it indicates that the method returns an
                 integer value to the calling method. “public” means that the method can be accessed from anywhere and the body of
                 the method prints twice the value of ‘n’.

                     9.3 PARTS OF A METHOD
                 As seen in the above syntax and example, there are various parts of a method which are as listed below:
                 •  Method Header
                 •  Method Signature
                 •  Access Specifier
                 •  Return Type and Return Statement
                 •  Method Name
                 •  Parameter List
                 •  Body of the Method
                 Let us study these parts of a method in detail:


                 9.3.1 Method Header
                 The first line of any method definition is known as the method header or method prototype. It consists of the access
                 specifier, return data type, method name and the parameter list.

                 Let us consider the following example:

                                              public  int  twice  (int n)


                                                                           Parameter List
                                                                           Method Name
                                                                           Return Type
                                                                           Access Specifier



                 9.3.2 Method Signature
                 The name of the method along with its parameter list in the method header is called the method signature. It is the
                 identification of the method in the programming code, which also specifies the different parameters used in that
                 method, which are known as arguments.

                                                             twice (int i)





                                                                                                                       217
                                                                                              Methods and Constructors  217
   214   215   216   217   218   219   220   221   222   223   224