# User-defined functions

• 04-10-2007
naya22
User-defined functions
Hi.

I am trying to write a program that asks for the wholesale cost of an item and its markup percentage, and displays the retail price.

Well, I did that the regular way, but...
My instructions require for me to:
1. Create a function that accepts the wholesale cost and markup percentage as arguments
2. Returns the retail price of the item
3. Do not accept negative values for either the wholesale cost of the item or the percent markup.

I have read my book, but I have no idea how to create a function for wholesaleCost and markupPrice. I tried, but I got an error.

Here is my regular program (without the user-defined function):
Code:

```#include <iostream> #include <iomanip> #include <conio> using namespace std; int main() {         double wholesaleCost, markupPercentage, markupAmount, retailPrice;         cout << "What is the wholesale cost of the item? ";         cin  >> wholesaleCost;         cout << "What is the markup percetage for this item? ";         cin  >> markupPercentage;         markupAmount = wholesaleCost * markupPercentage;         retailPrice = wholesaleCost + markupAmount;         cout << fixed << showpoint <<setprecision(2);         cout << "Your retail price for this item is \$" <<retailPrice <<endl;         getch();         return 0; }```
Does anyone have any idea how I can do this?? Any input is appreciated.
• 04-10-2007
laserlight
Basically, you want to write a function that has two parameters of type double, returns a double, and performs validation of its arguments by checking that they are non-negative.
• 04-10-2007
naya22
Yep, that's basically it.

I was thinking of something like:

Code:

```void wholesaleCost(double); void markupPercentage(double);```
I don't know how to perform the validation part.
• 04-10-2007
laserlight
You are creating two functions that each take one parameter of type double, and which return void. That is very different from one function that takes two parameters of type double, and which returns a double.
• 04-10-2007
naya22
I am confused now. I'm sorry. I'm new at this.

So, what do I do??
• 04-10-2007
laserlight
You could try reading cprogramming.com's tutorial on functions.
• 04-10-2007
naya22
Okay, thanks.