I had this same question. After some trial and errors, and reading about the common block, I made this short piece of code to demonstrate the use of global variables:
pro mysub
COMMON SHARE, x, y, z ; "SHARE" is just a word I made up. You can call it anything
print, 'goodbye'
print, x
end
pro GlobalVariables
COMMON SHARE, x,y,z ; You must reuse the word "SHARE" here. You can change the x,y,z to anything you like
print, 'hello'
x=2
mysub
end
Save this as "GlobalVariables.pro", not as "mysub.pro". When yo run it, your output should be:
hello
goodbye
2