|
Some beginners are uncomfortable with parameters. Look at this function definition.
// sqr(x) returns the square of x. int sqr(int x) { return x*x; }A reasonable question is, what is x? How can I multiply x times itself if I don't know what x is?
The answer is that the responsibility for choosing a value for x lies with the function call, not with the function definition. Statement
int w = sqr(20);
chooses value 20 for x. Another function call,
int n = sqr(40);
chooses a different value for x.
It is crucial that you learn to compartmentalize, to think only about a small piece of a computer program at a time. When writing a function definition, don't think about where its parameters will come from. That is not your problem. It will be handled someplace else.
|