The assumption you made is that the variable you have defined is global...but it ain't. Keep the following in mind...
1. Variables cannot be used in functions unless they are passed in as an argument.
2. Variables passed in as arguments and changed are not passed back to the calling statement
3. Functions can only return one value.
The code above from FlashingBlade is the definitive way to do it...get your head around the syntax because it will be the basis of every function you ever write, and you will no doubt write hundreds in the months to come.
I hope this helps.
Thanks in advance.
All the Best,
StevieVee