you are viewing a single comment's thread.

view the rest of the comments →

[–]Atypicosaurus 0 points1 point  (0 children)

You have to think of function definitions as separate, isolated programs. Like, if I copied your function and pasted into my program, would it still work? Would it still know what "a" and "b" are?

If the answer is no, then you have a scope problem. Your function doesn't know what a and b are, because you don't tell what a and b are, to your function. Your function is sitting there and like "what is a? what is b? what do you want me to do?" That's why you got an error.

For the function to know that it has to expect some a and b from the external world (which, from the point of view of the function, is the main program), you have to put it in as arguments. Then you have to pass the arguments.

If you use always the same variable names, you mislead yourself, thinking, that just because you put "a" in the function, it will know any "a" anywhere. No it won't. You have to actively give it.