{"id":"return-values-call-stack","slug":"the-call-stack","title":"The call stack","text":"When a method calls another, Java has to remember where to come back to. It keeps a **call stack**: a pile of the methods that are running right now. A call puts the new method on top, with its own parameters and variables. When that method returns, it's taken off the pile, and the method below carries on right after the call.\n\n```java\npublic class Main {\n    public static void main(String[] args) {\n        System.out.println(\"main starts\");\n        int result = half(10);\n        System.out.println(\"main got \" + result);\n    }\n\n    public static int half(int number) {\n        System.out.println(\"half starts with \" + number);\n        return number / 2;\n    }\n}\n```\n\n```output\nmain starts\nhalf starts with 10\nmain got 5\n```\n\nWhile `half` runs, the stack holds `half` on top of `main`. `main` is paused in the middle of the line `int result = half(10);`, and it finishes that line with the returned 5.\n\nYou may have seen the call stack already: when a program crashes, Java prints it. Here `share` divides by zero:\n\n```\nException in thread \"main\" java.lang.ArithmeticException: / by zero\n\tat Main.share(Main.java:8)\n\tat Main.main(Main.java:4)\n```\n\nEach `at` line is one method on the stack, with the line it was on, the top one first: the crash happened in `share` on line 8, which was called from `main` on line 4.\n\nThe challenges in this step are for tracing: follow the calls and the returns, and write down what's printed, in order.\n","task":"What does this program print?\n","kind":"predict","seed":"public class Main {\n    public static void main(String[] args) {\n        System.out.println(\"A\");\n        greet();\n        System.out.println(\"C\");\n        greet();\n    }\n\n    public static void greet() {\n        System.out.println(\"B\");\n    }\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        System.out.println(\"A\");\n        greet();\n        System.out.println(\"C\");\n        greet();\n    }\n\n    public static void greet() {\n        System.out.println(\"B\");\n    }\n}\n","hints":["Each call to `greet` prints B, and then `main` carries on from where it was.","`main` prints A, calls greet (B), prints C, and calls greet again (B)."],"tests":[{"name":"Output","stdin":"","expect":"A\nB\nC\nB","hidden":false}],"require":[],"forbid":[],"lines":["A","B","C","B"],"more":[{"task":"What does this program print? Three methods call each other.\n","kind":"predict","seed":"public class Main {\n    public static void main(String[] args) {\n        System.out.println(\"main starts\");\n        first();\n        System.out.println(\"main ends\");\n    }\n\n    public static void first() {\n        System.out.println(\"first starts\");\n        second();\n        System.out.println(\"first ends\");\n    }\n\n    public static void second() {\n        System.out.println(\"second\");\n    }\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        System.out.println(\"main starts\");\n        first();\n        System.out.println(\"main ends\");\n    }\n\n    public static void first() {\n        System.out.println(\"first starts\");\n        second();\n        System.out.println(\"first ends\");\n    }\n\n    public static void second() {\n        System.out.println(\"second\");\n    }\n}\n","hints":["`first` doesn't end until `second` has finished.","The order is: main starts, first starts, second, first ends, main ends."],"tests":[{"name":"Output","stdin":"","expect":"main starts\nfirst starts\nsecond\nfirst ends\nmain ends","hidden":false}],"require":[],"forbid":[],"lines":["main starts","first starts","second","first ends","main ends"]},{"task":"What does this program print? A value is returned through two calls.\n","kind":"predict","seed":"public class Main {\n    public static void main(String[] args) {\n        int result = outer(3);\n        System.out.println(\"Result: \" + result);\n    }\n\n    public static int outer(int n) {\n        int value = inner(n + 1);\n        System.out.println(\"outer got \" + value);\n        return value * 2;\n    }\n\n    public static int inner(int n) {\n        System.out.println(\"inner got \" + n);\n        return n * 10;\n    }\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        int result = outer(3);\n        System.out.println(\"Result: \" + result);\n    }\n\n    public static int outer(int n) {\n        int value = inner(n + 1);\n        System.out.println(\"outer got \" + value);\n        return value * 2;\n    }\n\n    public static int inner(int n) {\n        System.out.println(\"inner got \" + n);\n        return n * 10;\n    }\n}\n","hints":["`outer(3)` calls `inner(4)`, which prints first and returns 40.","Then `outer` prints what it got and returns 80 to `main`."],"tests":[{"name":"Output","stdin":"","expect":"inner got 4\nouter got 40\nResult: 80","hidden":false}],"require":[],"forbid":[],"lines":["inner got 4","outer got 40","Result: 80"]}]}