5-9 ValueReturn

Post date: Mar 29, 2014 5:42:33 PM

//This program demonstrates a value-returning Method.

package valuereturn;

public class ValueReturn

{

        public static void main(String[] args)

        {

            int total, valuel = 20, value2 = 40;

            // Call the sum method, passing the contents of valuel and valuo2 as arguments.

            // Assign the return value to the total variable.

            total = sum(valuel, value2);

            // Display the contents of all these variables.

            System.out.println("The sum of " + valuel + " and " + value2 + " is " + total) ;

        }

        /* 

           The sum method returns the sum of its two parameters.

           @param numl The first number to be added.

           @param num2 The second number to be added.

           @return The sum of numl and num2.

        */

        public static int sum(int numl, int num2)

        {

            int result;   // result is a local variable

            // Assign the value of numl + num2 to result.

            result = numl + num2;

            // Return the value in the result variable.

            return result;

        }

    

}