Reputation: 19
currently i study in c programming language but i have a problem, is there i can get one value from multiple argument in one function and print it in main function in c?? i already create a function that come out with two argument, but somehow i need to print just one argument that only appear one process that i create more than one operation in main function.
this is my code:
#include<stdio.h>
float bakeryShop(float price_a, float price_b)
{
float discountA, discountB;
discountA = price_a - (price_a*45/100);
discountB = price_b - (price_b*60/100);
printf("%.2f", discountA);
return (discountA);
printf("%.2f", discountB);
return (discountB);
}
main()
{
char name_item_a[200];
char name_item_b[200];
float price_a, price_b;
float discountA, discountB, result;
printf("Enter item A: ");
scanf("%s", &name_item_a);
printf("Enter price for item A: ");
scanf("%f", &price_a);
printf("Enter item B: ");
scanf("%s", &name_item_b);
printf("Enter price for item B: ");
scanf("%f", &price_b);
printf("%s\n", name_item_a);
bakeryShop(price_a, price_b);
printf("%.2f", result);
return price_a;
printf("%s\n", name_item_b);
result = bakeryShop(price_a, price_b);
printf("%.2f", result);
return price_b;
}
if you can see in the bakeryShop function, i create two process which is discountA and discountB but how to display only discount A in main function ??
because the output should be like this,
ITEM A
Name:
Original Price: RM
Price after 45% discount: RM
ITEM B
Name:
Original Price:
M Price after 60%
discount: RM
but i do not know how to get only one process discount A seperate with another process discount B
Upvotes: 1
Views: 219
Reputation: 334
Your question is kind of confusing, but if I understand it correctly, then you're basically trying to calculate the discounted price of two "items" based on user input, where each item has a unique discount (price/45*100
for item A
, price/60*100
for item B
) - and you're stuck on trying to return the discount for both items from the function that's supposed to calculate it.
I'd approach the problem differently. Since both items A and B are defined by the same set of information (a name, a price, and a later calculated discounted price) then why not group that information together into an "Item"
struct?
Another thing, since the discount is specific to each item, rather than making a function that takes multiple items as arguments, and calculates the discount for each of them separately (spawning the problem of returning multiple values, and nullifying the point of a function as you'll have to expand or contract the function dependiong on how many items you need to calculate in main) why not make a function that calculates the discount of a single item at a time, with the discount specified as an argument?
#include <stdio.h>
struct Item {
char Name[200];
float Price;
float DiscountedPrice;
};
void ApplyDiscount(Item* item, float amount) {
item->DiscountedPrice = (item->Price * amount) / 100.0;
}
// Or, if you haven't studied pointers yet..
Item ApplyDiscount(Item item, float amount) {
item.DiscountedPrice = (item.Price * amount) / 100.0;
return item;
}
int main() {
Item item_a, item_b;
printf("Item A name: ");
scanf("%s", &item_a.Name);
printf("Item A price: ");
scanf("%f", &item_a.Price);
printf("Item B name: ");
scanf("%s", &item_b.Name);
printf("Item B price: ");
scanf("%f", &item_b.Price);
ApplyDiscount(&item_a, 45);
ApplyDiscount(&item_b, 60);
printf("%s: %.2f\n", &item_a.Name, item_a.DiscountedPrice);
printf("%s: %.2f\n", &item_b.Name, item_b.DiscountedPrice);
}
If you'd only like to know how to return multiple values from a function, there are multiple ways. One such way would be to write the values through pointers passed as arguments, like so
void ApplyDiscount(float item_a_price, float item_b_price, float* item_a_result, float* item_b_result) {
*item_a_result = (item_a_price * 45.0) / 100.0;
*item_b_result = (item_b_price * 60.0) / 100.0;
}
int main() {
float item_a_price = 180;
float item_b_price = 42;
float item_a_result, item_b_result;
ApplyDiscount(item_a_price, item_b_price, &item_a_result, &item_b_result);
}
But I wouldn't really advise this, since it can get pretty messy pretty quickly.
You could also just group the results together in a struct, but if you're doing it like that anyway, then you may as well just make an "Item"
type like I previously suggested.
struct Results {
float ResultA;
float ResultB;
};
Results ApplyDiscount(float item_a_price, float item_b_price) {
Results results;
results.ResultA = (item_a_price / 45.0) * 100.0;
results.ResultB = (item_b_price / 60.0) * 100.0;
return results;
}
int main() {
float item_a_price = 180;
float item_b_price = 42;
float item_a_result, item_b_result;
Results results = ApplyDiscount(item_a_price, item_b_price);
item_a_result = results.ResultA;
item_b_result = results.ResultB;
}
Upvotes: 1
Reputation: 1484
I do believe you want to define the function like this:
float bakeryShop(float price, float discount_percent)
{
float discount;
discount = price - (price*discount_percent/100);
printf("%.2f", discount);
return discount;
}
and then use it like:
printf("%s\n", name_item_a);
result = bakeryShop(price_a, 45);
printf("%.2f", result);
printf("%s\n", name_item_b);
result = bakeryShop(price_b, 60);
printf("%.2f", result);
Because what you did in the code above doesn't work in C at all - you cannot return multiple times from a function. If you return, you return from function once, and that's it.
So, use this function for every item separately, and it'll be usefull and meaningfull in terms of C.
Upvotes: 2
Reputation: 332
You can not return 2 variables from a function like this.
float bakeryShop(float price_a, float price_b)
{
return (discountA);
return (discountB);
}
This will only return the first variable. As price_a and price_b are not connected - you can just take only 1 argument in the function. Then calling the function twice from the main method. Something like this -
float bakeryShop(float price)
{
float discount;
discount = price - (price*45/100);
return (discount);
}
int main(){
int price1, price2;
scanf("%d, %d", &price1, &price2);
float discount1 = bakeryShop(price1);
float discount2 = bakeryShop(price2);
printf("%.2f", discount1);
printf("%.2f", discount2);
}
Upvotes: 1