Page 228 - CA_Blue( J )_Class10
P. 228

10.4 WAYS TO DEFINE A METHOD
              There are two ways to define a method:
              1.  Methods may or may not take values as parameters:
                  a.  Methods not taking parameters:
                      void sum( )
                      {
                          s = a + b;
                          System.out.println("The result is:" +s);
                      }
                  b.  Methods taking parameters:
                      void accept(int m, int n)
                      {
                          a=m; b=n;
                      }
              2.  Methods may or may not return values:
                  a.  Methods not returning values: If a function is not returning any value, then the word "void" is used.

                      void sum ()
                      {
                          s=a + b;
                          System.out.println("The result is:" +s);
                      }
                  b.  Methods returning value of primitive data type:

                      int sum (int a, int b)
                      {
                          s=a + b;
                          return s;
                      }
                  c.  Methods returning value of non-primitive data type:

                      int[] sum ()
                      {
                          int i;
                          int ar[]={1,2,3,4,5};
                          for(i=0;i<5;i++)
                      {
                          ar[i]=ar[i]+2;
                      }
                      return ar;
                      }

                   10.5 ACTUAL AND FORMAL PARAMETERS
              There are two types of parameters in a Java program:
              •  Actual Parameters: The actual values that are passed directly or through variables to the respective method at the
                  time of calling the method are called actual parameters. These are defined in the calling program.
              •  Formal Parameters: The values send by the calling  program are received in the parameters described in the
                  method. The parameters that receive the values from the caller program are known as formal parameters.










                226226  Touchpad Computer Applications-X
   223   224   225   226   227   228   229   230   231   232   233