But how does a computer really do a function call? The expression(), term(), and primary() functions from Chapters 6 and 7 are perfect for illustrating this except for one detail: they don’t take any arguments, so we can’t use them to explain how arguments are passed. But wait! They must take some input; if they didn’t, they couldn’t do anything useful. They do take an implicit argument: they use a Token_stream called ts to get their input; ts is a global variable. That’s a bit sneaky. We can improve these functions by letting them take a Token_stream& argument. Here they are with a Token_stream& parameter added and everything that doesn’t concern function call implementation removed. First, expression() is completely straightforward; it has one argument (ts) and two local variables (left and t):
double expression(Token_stream& ts)
{
double left = term(ts);
Token t = ts.get();
// . . .
}
Second, term() is much like expression(), except that it has an additional local variable (d) that it uses to hold the result of a divisor for '/':
double term(Token_stream& ts)
{
double left = primary(ts);
Token t = ts.get();
// . . .
case '/':
{
double d = primary(ts);
// . . .
}
// . . .
}
I just wanna know what the point of doing this exactly was? Before making the functions take a reference argument ts was just a global object and the functions looked like this:
double term()
{
double left = primary();
Token t = ts.get();
// . . .
case '/':
{
double d = primary(ts);
// . . .
}
// . . .
}
Like just what is the point of doing this? We didn't need arguments why did the writer decide to add reference arguments to each function?