Page 171 - CA_Blue( J )_Class10
P. 171

8.8.3 The return Statement
                 In Java, when a method is called, the control is moved from the caller module to the method which has to return back
                 to the caller module after the execution of the method. This is done by the return statement. If the method does not
                 return any value, we have to use the keyword "void" else we will use the data type of the returned value. Syntax of the
                 return statement is:

                 return function_name(arguments)
                 {
                     statements;
                     return value or expression;
                 }

                  Program 10 To print the sum of 4 and 2 using the return statement.


                   1   class cal
                   2   {

                   3       int sum(int a,int b)
                   4       {

                   5           int s= a+b;
                   6           return s;

                   7       }
                   8       void main()

                   9       {
                  10           System.out.println(sum(4,2));

                  11       }
                  12   }

                 You will get the following output:


















                 We will learn more about the return statement in Chapter "User-defined Methods".













                                                                                                                       169
                                                                                              Iterative constructs in Java  169
   166   167   168   169   170   171   172   173   174   175   176