SE450: Basics: Functions [23/63] Previous pageContentsNext page

What is a function?

file:Main.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
package basics.functions;
public class Main {
  private Main() {}
  static public void main (final String[] args) { 
    //stdlib.Trace.graphvizShowSteps (true); stdlib.Trace.run ();
    Fun.f();
    Fun.g(2);
  }
}
class Fun {
  private Fun() {}
  static void f() { System.out.println("Fun.f()"); }
  static void g(final int x) {
    System.out.println("Fun.g(" + x + ")");
    if (x>0) {
      final int y = x-1;
      Fun.g(y);
    }
  }
}

Here is a visualization of the execution at four different point (time moving from left to right).

trace-basics-functions-003-Fun_f_12 trace-basics-functions-008-Fun_g_17 trace-basics-functions-012-Fun_g_17 trace-basics-functions-015-Fun_g_19

Previous pageContentsNext page