# return statement

This is a discussion on return statement within the C Programming forums, part of the General Programming Boards category; Hi, Need little help on my homework assignment. Let's say I have the information on three stocks which include stock ...

1. ## return statement

Hi,

Need little help on my homework assignment. Let's say I have the information on three stocks which include stock name, buying price, current price, number of shares bought and and yearly fees.

I am supposed to calculate the following:

1. initial cost which is number of share * buying price
2. current cost, which is the number of shares * current price
3. profit, which is current_cost - initial_cost - yearly_fees
4. total profit = total + profit.

I also need have the program figure out how many of the three stocks are generating positive profit, negative profit or break even and print out the number of each.

Another requirement is to write with appropriate functions and paramenter passing (the reference/value stuff) and to use one void function and one function with return statement.
-------------------

I have completed assignment using only void functions, but wondering how to incorporate the return statement deal?

2. Are you working with structures? If so do you know how to access the struct members? Each structure you create can represent each stock ticker you are working with.

3. I haven't learned structures in class yet, therefore can't use it. Though as far as I understand from my book, structures make great for writing code to make databases.

But what can I do? Still need help figuring out a function that involves using return statement.

4. Originally Posted by x2x3i5x
I haven't learned structures in class yet, therefore can't use it. Though as far as I understand from my book, structures make great for writing code to make databases.

But what can I do? Still need to incorporate that return statement.
(1) In your own terms, can you explain what the return statement does?
(2) Could we see your code for the function that calculates profit, f.ex.?

5. Here's my entire program code. Return statement basically feeds out a value to the main from the function so that main can use it. I attempted doing return *profit, but obviously that's just a wasted line of code as it does nothing. So here I am. Yeah, in the main I could've used a loop, but loops are saved for a later assignment so it's not implemented here.

Code:
```#include <stdio.h>

void input(char *stock_name, char *purchase_date, int *num_shares, float *buying_price, float *current_price, float *yearly_fees)
{
printf("Enter stock name: ");
gets(stock_name);

printf("Enter purchase date: ");
gets(purchase_date);

printf("Enter number of shares: ");
scanf("%d", &(*num_shares));

printf("Enter buying price per share: ");

printf("Enter current price per share: ");
scanf("%f", &(*current_price));

printf("Enter yearly fees: ");
scanf("%f", &(*yearly_fees));

fflush(stdin);
}

float calcProfit(float buying_price, float current_price, float *yearly_fees, float *initial_cost, int *num_shares, float *current_cost, float *profit, float *total)
{
*current_cost = *num_shares * current_price;
*profit = *current_cost - *initial_cost - *yearly_fees;
*total = *total + *profit;

return *profit;
}

void calcProfit2(float profit, int *pos_profit, int *neg_profit, int *even)
{
if (profit>0)
(*pos_profit)++;
else if (profit<0)
(*neg_profit)++;
else
(*even)++;
}

void print1(char *stock_name, char *purchase_date, float initial_cost, float current_cost)
{
printf("\nStock Name: %s\n", stock_name);
printf("Purchase Date: %s\n", purchase_date);
printf("Initial Cost: \$%0.2f\n", initial_cost);
printf("Current Cost: \$%0.2f\n", current_cost);
}

void print2(float total, int pos_profit, int neg_profit, int even)
{
printf("Total Profit: \$%0.2f\n", total);
printf("Number of stocks with positive profit: %d\n", pos_profit);
printf("Number of stocks with negative profit: %d\n", neg_profit);
printf("Number of stocks broke even: %d\n", even);
}

void main()
{
char stock_name[9], purchase_date[9];
int num_shares, pos_profit=0, neg_profit=0, even=0;
float buying_price, current_price, yearly_fees, initial_cost=0, current_cost=0, profit=0, total=0;

input(stock_name, purchase_date, &num_shares, &buying_price, &current_price, &yearly_fees);
calcProfit(buying_price, current_price, &yearly_fees, &initial_cost, &num_shares, &current_cost, &profit, &total);

calcProfit2(profit, &pos_profit, &neg_profit, &even);
print1(stock_name, purchase_date, initial_cost, current_cost);
printf("Profit: \$%0.2f\n\n", profit);

input(stock_name, purchase_date, &num_shares, &buying_price, &current_price, &yearly_fees);
calcProfit(buying_price, current_price, &yearly_fees, &initial_cost, &num_shares, &current_cost, &profit, &total);
calcProfit2(profit, &pos_profit, &neg_profit, &even);
print1(stock_name, purchase_date, initial_cost, current_cost, profit);

input(stock_name, purchase_date, &num_shares, &buying_price, &current_price, &yearly_fees);
calcProfit(buying_price, current_price, &yearly_fees, &initial_cost, &num_shares, &current_cost, &profit, &total);
calcProfit2(profit, &pos_profit, &neg_profit, &even);
print1(stock_name, purchase_date, initial_cost, current_cost, profit);

print2(total, pos_profit, neg_profit, even);
}```

6. Have you covered arrays yet?

7. Bo sorry, haven't learned on arrays except for the char arrays, so a way to go for it?

8. I suspect with this assignment, your instructor is setting you up to teach structures - so you will see how nice they are to "package" related data together.

Back to your current issue - you could have an array for each of your variables, stock_name, purchase_date, etc., etc., etc... That would be how I would do it. With all the data kept in arrays, it makes it easier to iterate through all the data to find, for instance, which stocks are good performers and which are dogs. You can sort, sum, do all kinds of operations if you keep the data around and don't overwrite the previous values each time you get new info from the user.

9. ok, but the return statement function?

10. Originally Posted by x2x3i5x
Here's my entire program code. Return statement basically feeds out a value to the main from the function so that main can use it. I attempted doing return *profit, but obviously that's just a wasted line of code as it does nothing.
More specifically, the return statement returns a value to the calling function for future use. It doesn't have to be main().

The return *profit; does nothing because you throw away the result. I.e. you do not assign the returned value to a variable. Also, you are saying that the return type for calcProfit() is float but you are returning a pointer to float.

11. Originally Posted by msh
More specifically, the return statement returns a value to the calling function for future use. It doesn't have to be main().

The return *profit; does nothing because you throw away the result. I.e. you do not assign the returned value to a variable. Also, you are saying that the return type for calcProfit() is float but you are returning a pointer to float.
I realized that the *profit return is just junk, but how else to fix the problem? Maybe profit is not the right variable to have the result returned back.