Write a program to return a value from a method.

This program will explain how to return a value from a method.

Steps involved:

1. Create a class.
2. Create a method().
3. Create a main() method, and we will call created method() from main()     method.



class Test
{
public static void main(String args[])
{
//here we will initialize value of n. 
 int n =50;
//storing returned value from myMethod into res integer.
//returned value will be stored in res int
int res = myMethod(n);
//printing res value 
System.out.print("Returned value from myMethod: "+res);
}
//created method
static int myMethod(int n)
{
 return n*n;
}
} 
 
 

Output: 

Returned value from myMethod: 2500

Previous Post Next Post

Contact Form