Sample Program:
Case: This is an implementation of previous sample program. Instead of printing value, ‘multiplyMethod’ will return the result back to main method.
public class myHomeWorksReturn
{
public static void main(String[] args)
{
int value1 = 4;
int value2 = 2;
int ReceivedValue;
//declaring a variable that can hold values from multiplyMethod
ReceivedValue = multiplyMethod(value1, value2);
//Calling to method with arguments
System.out.println("Received Value is: " + ReceivedValue);
}
public static int multiplyMethod(int number1, int number2)
{
int Result = number1 * number2;
return Result; //returns value back
}
}
This is the output:
No comments:
Post a Comment