SLIDE 4 11/6/2013 4
Example: Dynamic Scoping
$x = 5; $y = 10; myFunction(); # call subroutine– no argument print "x = $x, y = $y\n"; sub myFunction { local($x, $y); #make it my ($x,$y) to swap global var $x = 100, $y = 200; swap(); # no argument print "After swap: x = $x, y = $y\n"; } sub swap { $t = $x; #Is this the x in myFunciton or the global x? $x = $y; $y = $t; }
Type System
- Review [Caution: there are different definitions of these terms]
- A language is statically typed, if the types of all variables are fixed when
they are declared at compile time
- A language is dynamically typed if the type of a variable can vary at run
time depending on the value assigned
- A language is strongly typed if its type system can detect all type errors
either at compile time or at run time
- “Weakly typed” is not well-defined – may not be related to strongly typed
- Examples
- C is statically typed, but not strongly typed (hint: pointer, array index)
- Java is both statically and strongly typed
- Perl is both dynamically and strongly typed