"OK, let's try another approach. I'll show you how calling methods works, and then you try to go through the previous lesson again, OK?"

"Let's do it."

"Great. I'll tell you about calling functions/methods and the values they return (return values)."

"Commands, or statements, are grouped into methods so they can be executed as a single block, like a single complex command. To do this, you need to write the method (function) name and then list the method's arguments inside parentheses."

Example
package com.codegym.lesson2;
public class MethodCall
{
    public static void main(String[] args)
    {
         print4("I like to move it, move it.");
    }

    public static void print4(String s)
    {
        System.out.println(s);
        System.out.println(s);
        System.out.println(s);
        System.out.println(s);
    }
}

"In the example above, we wrote a function that will display the passed string on the screen four times. Then we called the print4 function in line 6."

"When the program reaches line 6, it will jump to line 9, assigning the value 'I like to move it, move it' to the variable s."

"Then lines 11-14 will be executed. The function will finish, and the program will resume on line 7."

"I see."

"Not only can you pass arguments (values) to a function—a function can return the result of its work (return value). This is done with the keyword return. This is how it looks:"

Example 1.
Determine the minimum of two numbers.
public class MethodCall
{
   public static void main(String[] args)
   {
      int a = 5, b = 7;
      int m = min(a, b);
      System.out.println("The minimum is "+ m);
   }

   public static int min(int c, int d)
   {
      int m2;
      if (c < d)
           m2 = c;
      else
           m2 = d;

      return m2;
   }
}
Here's how it works:
public class MethodCall
{
   public static void main(String[] args)
   {
      int a = 5, b = 7;
      int c = a, d = b;
      int m2;
      if (c < d)
           m2 = c;
      else
           m2 = d;

      int m = m2;
      System.out.println("The minimum is "+ m);
   }
}

"I think it's starting to make sense! The code in the left and right columns is actually the same. It's just that the code on the left has a standalone function."

"The function computes a certain value and uses the return statement to pass that value to whatever called it. At least, that's how I see it."

"And you're right!"

"But what is this void type?"

"Some functions just do something without computing or returning any value, like our main() method. A special return type–void–was created for such functions."

"Why not just declare nothing if a function doesn't return anything?"

"Remember how we declare any variable? Type and name. For functions, we declare a type, name, and parentheses. A function name followed by parentheses is how you call the function."

"So, it was easier to invent a 'void type' than to divide functions into two categories – those that return values and those that do not?"

"Exactly! You're really smart, my boy."

"How do we return a void type?"

"We don't. It works like this. When executing the return statement, the Java machine computes the value of the expression to the right of the word 'return', stores this value in a special part of the memory, and immediately ends the function. The stored value is used where the function was called, as the result of calling the function. You can see it in the example I gave earlier."

"You mean the part where int m = min(a, b) is transformed into m = m2?"

"Yes. After the function is finished, everything works as if the function's return value was written in its place. Repeat this phrase in your mind and look at the code in the last example."

"I think this only seems easy. It's actually difficult. I've only understood parts of it."

"That's OK. On the first try, you can only understand things you already know. The more things you don't understand, the deeper you are plunging into something new, and the better your results will be. It will become clearer with time."

"Well, if you say so. Let's keep going."