5-9 ValueReturn

posted Mar 29, 2014, 10:42 AM by Dlovan ismail
//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;

        }
    
}

ċ
ValueReturn.java
(1k)
Dlovan ismail,
Mar 29, 2014, 10:42 AM
Comments