Confused about scope

What I’m confused about is the status of variables in modules, as seen inside functions. The first example seems to suggest they are global, but then why can’t I modify them in the second example? The third example is surprising (adding a definition of a later in the function makes it undefined!) but makes sense, I agree.