Creating a Function - variable not decleared

Creating a Function - variable not decleared

Hello,
I have a script to calculate prices in a subform. This script is duplicated in three fields price, quantity and discount.
I want to create a custom function called 'Price calc' so that I can alter the script in one place.

To create the function I'm simply copying and pasting the script from one of the fields, into the Function script area.
However I get several variable  undeclared error when I try to save the function.

What am I doing wrong.
Thanks
Stephen