Page 245 - ComputerScience_Class_11
P. 245

•  They also occupy less space in memory and the execution becomes faster.
                 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
                 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.
                 Example:


                                                             twice (int i)




                                                                                           Methods and Constructors  243
   240   241   242   243   244   245   246   247   248   249   250