use this:
function foo { typeset x=2 # Makes x local to the function } x=1 foo echo $x # This will print 1