Getting variables value from inside function

I’m pretty new to AS, I understand that whatever variables you have inside a function will be discarded once the function finishes?

I need the value of a variable that’s inside a function, I’ve tried declaring it outside the function, and also give it an initial value, but it seems that when the function finishes the variable goes back to it’s original value.