Auto code execution on exiting a function scope