Page 229 - Cs_withBlue_J_C11_Flipbook
P. 229

45 34 26 67 78 98
                   -------------------------------
                   Result before increase by 2
                   45 34 26 67 78 98
                   -------------------------------
                   The result after an increase by 2
                   47 36 28 69 80 100
                   -------------------------------
                 Values of original array arr[] in main() after calling sum():

                 47 36 28 69 80 100
                 The above example followed the sequence given below.
                 •  From main(), array ar when printed we get, 45 34 26 67 78 98
                 •  Then sum(arr) method is invoked
                 •  In the sum(int ar) method before increasing by 2, the values printed are 45 34 26 67 78 98
                 •  After increasing by 2, the values printed are 47 36 28 69 80 100
                 •  At last, when the array is printed after the call of sum(), we get 47 36 28 69 80 100 in the array ar even though the
                   values are not returned.
                 So, in pass by reference any change to the formal parameters will also reflect on the actual parameters. Also, only non-
                 primitive data can be used in pass by reference.

                     9.7 PURE METHOD AND IMPURE METHOD
                 Pure and impure methods are two programming terms we often see in functional programming. Let us read about
                 them in detail.

                 9.7.1 Pure Method
                 A method that does not change the values passed to it, is known as a pure method. In other words, there is no change
                 in the state of the function, and thus has no adverse effect on the results A pure method is a returnable method. It is
                 also called as accessor method.

                 Let us take the following example.
                    class pure_method
                    {
                        int add(int m1)
                        {
                            m1=(m1+5)*5;
                            return m1;
                        }

                        void main()
                        {
                            int marks=50, total;
                            total = add(marks);
                            System.out.println("Actual parameter : "+marks);
                            System.out.println("Returned value : "+total);
                        }
                    }
                 The output of the preceding program is as follows:
                   Actual parameter : 50





                                                                                                                       227
                                                                                              Methods and Constructors  227
   224   225   226   227   228   229   230   231   232   233   234