help with creating function from snippit

Fix your posts to show the code correctly. Then we'll look at it. I've got some ideas what may be wrong but I can't be sure when it's posted like that.