Your problem has to do with variable scope. The function doit() has it's own scope, so variables declared outside of that function are not accessible from within that function. The best way to provide access to external variables in a function is to pass them as parameters.
EDIT: For more information/examples, see this manual page: [man]variables.scope[/man].