# Quickstart: Custom function

You can use Google Apps Script to write a custom function, then use it in Google Sheets just like a built-in function.

The below quickstart sample creates a custom function that calculates the sale price of discounted items. The sale price is formatted as US dollars.

## Set it up

### New editor

1. Create a new spreadsheet.
2. From within your new spreadsheet, select the menu item Extensions > Apps Script.
3. Delete any code in the script editor and paste in the code below.
```/**
* Calculates the sale price of a value at a given discount.
* The sale price is formatted as US dollars.
*
* @param {number} input The value to discount.
* @param {number} input The discount to apply, such as .5 or 50%.
* @return The sale price formatted as USD.
* @customfunction
*/
function salePrice(input, discount) {
let price = input - (input * discount);
let dollarUS = Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD",
});
return dollarUS.format(price);
}
```
1. At the top, click Save .

### Legacy editor

1. Create a new spreadsheet.
2. From within your new spreadsheet, select the menu item Extensions > Apps Script.
3. Delete any code in the script editor and paste in the code below.
```/**
* Calculates the sale price of a value at a given discount.
* The sale price is formatted as US dollars.
*
* @param {number} input The value to discount.
* @param {number} input The discount to apply, such as .5 or 50%.
* @return The sale price formatted as USD.
* @customfunction
*/
function salePrice(input, discount) {
let price = input - (input * discount);
let dollarUS = Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD",
});
return dollarUS.format(price);
}
```
1. Select the menu item File > Save. Name your new script and click OK.

## Try it out

2. In a cell, enter `=salePrice(100,.2)`. The first parameter represents the original price and the second parameter represents the discount percentage.
The function results in a sale price of `\$80.00`.