You need to read a basic tutorial about scope, functions, and passing arguments around as well as returning values. If you have trouble while you're learning that you're welcome to ask specific questions.