0

[Solved]I really need a little bit explain on parameter for function (Please HELP meTwT)

Can someone help me explain why While(StockSelect(stocknum)) need to have 'stocknum' in the parameter of the function 'StockSelect' as it didn't get any input value yet or is there any reasons for doing this? #My teacher show me this example code for my project #Thanks for your answer int main() { int stocknum; while(StockSelect(stocknum)) { int inqchoice = ShowStockMenu(); switch(inqchoice) { case 1: PriceDataInquiry(stocknum); break; case 2: GainLossCalculation(stocknum); break; default: cerr<<"Invalid inquiry choice!"<<endl; exit(EXIT_FAILURE); } } return 0; }

10/6/2019 12:01:15 PM

Yourint

5 Answers

New Answer

+3

Yourint You have provided incomplete information. Without looking at the prototype of StockSelect method it can be difficult to explain. So I am making an assumption here that the prototype of StockSelect function is like this bool/int StockSelect(int& val). If the prototype is like this then then the function is updating the value passed as argument by reference inside the function. Since the value is being passed by reference, then whatever modification is made to the value will be reflected back outside the function too. So you initially passed uninitialized stocknum(has garbage value) variable (as val), inside the function if i assign val = 10, then variable stocknum will also get value 10. Why is this done like that is probably the value say 10 is obtained by doing various other calculations and it was not possible or required to assign some initial value to it. At the end the function is returning one of true/false/0/1. If my explanation is wrong then you need to provide information about StockSelect

+2

See "Module 4: Functions" in the C++ Tutorial. Especially unit "35 Function Parameters" and unit "36 Functions with Multiple Parameters"

+1

~ swim ~ That's what I needed! Thank you!

+1

Martin Taylor Thank you I will go read some more:3

+1

Yourint 👍☺