Introduction

A function packages a useful operation under a name. Parameters supply input, and a return value carries the result back to the caller. Keep printing separate from calculation so you can reuse and test the function.

Syntax & example

public class Main {
  public static void main(String[] args) {
    System.out.println(doubleNumber(7));
  }
  static int doubleNumber(int number) { return number * 2; }
}

Expected output

14

Common mistakes

  • Using a variable before assigning a value to it.
  • Mixing text and numbers without converting deliberately.
  • Skipping an empty input or a boundary value when testing.

Practice

Write a function that doubles its input.

13px
Main.javaYour ideas, in code.
Loading your editor…
Run your code to see the output here.
Ready to buildUTF-86 lines