Global vs local keyword


I like to declare the global variables at the top of the file, using the keyword global. I experimented by replacing the keyword global with local. Nothing changed, the script continued to work the same way.

My question is: Does something change and I am missing it (by using local instead of global)?

In my files I like to use this statement, which forces me to declare all variables:
SetOption( "RequireDeclarations", True );

I have read this:

Due to the fact that AFL does not require to declare variables the decision whenever given variable is treated as local or global is taken depends on where it is FIRST USED.
If given identifier appears first INSIDE function definition - then it is treated as LOCAL variable.
If given identifier appears first OUTSIDE function definition - then it is treated as GLOBAL variable.
This default behaviour can be however overriden using global and local keywords (introduced in 4.36) - see example 2.

I think you can go through this ink and see if it clears the doubt. I don't have anything more to add.

1 Like

'local' has a meaning only when used inside the function. Using it outside the function is a user error, but harmless. The keywords are documented and you should use them as documented:

1 Like

@Tomasz and @travick thank you for clarifying it for me.