In our bottom-up approach to developing this program, now that we have a running, useful version of the code ( stock2.c), we can begin to think about adding features to the program to meet the spec given in our task.
The next part of the task we might add is to handle the data for how much we paid for the stock and compute the profit or loss we have on this portfolio. To begin this change, we go back to the algorithm and add the necessary steps:
Get the number of shares
Get the selling price
Get the purchase price
Print the data values
Compute the portfolio cost
Compute the portfolio value
Compute the profit
Print the result
We could now translate these new steps into C and modify the stock2 program. We need to declare additional variables for the purchase price, stock cost, and profit. We already know how to compute a value (or cost) given the number of shares and price.
This computation is exactly the same for the portfolio cost and the portfolio value. Just the data values (variables) we use are different. This is an excellent example of where we might consider writing a general function which can be given the data and can be used to do both computations.
I call this function stock_price(). We can write an algorithm for this function:
GIVEN: the number of shares of stock and a price as
whole, numerator, and denominator
RETURN: the extended price of this stock
Compute the stock price
return the value
and then code it in C:
float stock_price(int shares, int whole, int num, int denom)
/* Given: the number of shares, and stock price in whole, numerator
and denominator.
Return: the cost/value of this amount of stock.
*/
{ float value;
/* Compute the stock price */
value = shares * (whole + (float) num / (float) denom);
/* Return the value */
return value;
}
-The header of this function has more information than the header we had for main(). Besides its name (stock_price) it has:
-a type identifier before the name
This declares the type of data this function returns.
-a list of parameters
These declare the number and types of information to be passed to the function. They look like variable declarations - and they are. These are just like variable declarations in the body of the function (they tell the compiler to allocate cells to store this information and associate names with the cells), except, these variables will get values when the function is called.
-The function has another local variable, value.-The assignment statement to compute the extended price uses ONLY the variable names local to the function.-The value to be returned from the function is done with a return statement.
We can call this function in main() with a statement like:
portfolio_value = stock_price(shares,sell_whole,
sell_numer,sell_denomin);
We pass information to the function in the argument list. When the function is called, the value of each of the argument expressions is COPIED to the corresponding parameter list cell -
The code for the entire program is in stock3.c.
What happens when we try to compile? We need function prototypes. That's in the next lecture
stock3.c includes the code for this program. You should also visit the lecture on prototypes as well.