Java Methods

The word method in Java is essentially the same thing as the word function used in other languages like C and C++. A method is a function or subroutine. Every method in Java belongs to a class (this is like the C++ member function).

Why Write Methods?

  • Divide-and-conquer:  Break programs into smaller, more manageable pieces
  • Reusability
  • Avoid repetition of code in a program
  • Building Methods

    The structure of a method:
      modifier(s) returnType methodName(parameter list)
      {
        // method body (i.e. what it does, how it works)
      }
    
    The pieces:

    To return a value (from the body of a method with a non-void return type), use the keyword return:

      return expression;
    

    A return statement will force immediate exit from the method, and it will return the value of the expression to the caller.
    Note: A method with a non-void return type needs to return an appropriate value.

    Calling Methods

    A call is a command to execute a method, which transfers program execution to the code inside the method body. A call must use the method name and must pass in appropriate arguments, matching the expected number and types of parameters in the parameter list.

    Examples:


    A method with void return type:
      static void printGreeting(int age)
      {
        System.out.println("Hello, my name is Johnny");
        System.out.println("And I am " + age + " years old");
      }
    
    Sample calls:
      printGreeting(6);	// can pass in a literal number
    
      int x = 10;
      printGreeting(x);	// can pass in a variable of matching type
    
    Note: For a method with return type void, simply call the method as a single statement, ending with a semicolon.
    A method with non-void return type:
      static double Compute(int x, double y)
      {
        double result;
        result = x * 1.5 + y;
        return result;
      }
    
    Sample calls:
      double ans;
      ans = Compute(5, 3.6);   // returned value is assigned to ans
    
      // in this sample call, the returned value is printed by the 
      //  System.out.print statement
      int a = 16;
      double b = 3.4;
      System.out.print("Answer is " + Compute(a,b));
    
    Note: For a function with non-void return type, use the returned value by placing the function call in the middle of another statement, where that tye of value is valid. For example, a returned value can be assigned to a variable. The returned value replaces the call when evaluating what happens next in a statement with multiple operations.

    Scope

    Scope - the portion of code in which a variable is valid.

    Pass By Value

    Example

    Method:
      static int myMethod(int x, int y)
      {
        x = x * 2;
        System.out.println("x = " + x);
        y = y * 2;
        System.out.println("y = " + y);
        return x + y;
      }
    
    Sample call:
      int a = 5, b = 8, ans;
      ans = myMethod(a, b);
      System.out.println("ans = " + ans);
      System.out.println("a = " + a);
      System.out.println("b = " + b);
    
    Notice that the output of the code is:
      x = 10
      y = 16
      ans = 26
      a = 5
      b = 8
    
    Try this code yourself, here

    Code Examples