Show code where the name of the method’s actual arguments and formal parameters are different so students see first hand that these two items don’t have to match for code to execute and improve their understanding of scope.

  • Students understanding or misunderstanding about scope impacts what they believe the actual arguments have to be.

    • When students don’t have a good grasp on scope, they believe (perhaps unconsciously) that the actual arguments have to match the formal parameters.

  • Definitions:

    • Actual arguments: the values passed to the method.

    • Formal parameters: the names of the variables specified in the method header.

  • Students often don’t realize that these two things are different.

  • Consider the following Java code, Example A.
    Students might not realize the difference between the two variables named x.


public class Example1 {
    public static void main(String[] args) {
        int x = 10;
        Example1.foo(x); // the variables match
    }
    public static void foo(int x) {
        System.out.println(x);
    }
}


  • Now, consider this example, Example B.

    • Students may have an easier time seeing that:

      • x is the actual argument to the method foo, and

      • y is the formal parameter to the method foo.


public class Example2 {
    public static void main(String[] args) {
        int x = 10;
        Example2.foo(x); // it works not passing "y"
    }
    public static void foo(int y) {
        System.out.println(y);
    }
}

More about this tip

External Source

Interview with Richard Weiss.