Page 206 - computer science (868) class 11
P. 206

Returned value : 275
              After the execution of the program, the value in the actual parameter remains the same.


              8.7.2 Impure Method
              A method that causes a change in the state of an object is called an impure method. It is also known as a mutator. The
              arguments are always passed as reference type and may or may not return a value.

              Let us see the following example.
                  class impure_method
                  {
                      void increase_marks(int m1[])
                      {
                          int i;
                          for(i=0;i<m1.length;i++)
                          {
                              m1[i]=m1[i]+5;
                          }
                      }

                      void main()
                      {
                          int i;
                          int marks[]={45,98,98,56,34};
                          System.out.print("Marks before increase : \n");
                          for(i=0;i<marks.length;i++)
                          {
                              System.out.print(marks[i]+" ");
                          }

                          increase_marks(marks);
                          System.out.println("\nMarks increased : ");
                          for(i=0;i<marks.length;i++)
                          {
                              System.out.print(marks[i]+" ");
                          }
                      }
                  }
              The output of the preceding program is as follows:
              Marks before increase:
              45 98 98 56 34
              Marks increased:

              50 103 103 61 39
              The differences between pure and impure method are as follows:
                                      Pure Method                                 Impure Method

                      There is no change in the state of an object.  There is a change in the state of an object.
                      A pure method is a returnable method.        An impure method may or may not return a value.
                      It is also known as Accessor.                It is also known as Mutator.


                   8.8 METHOD OVERLOADING
              Sometimes, more than one method may have the same name. But this may lead to confusion for the compiler about
              the  function to be called. So, to remove this problem, there should be some difference in their arguments. So, these



                204204  Touchpad Computer Science-XI
   201   202   203   204   205   206   207   208   209   210   211