Why assign a function to a variable? What's the point in assigning int x, y to ReadNumber() ? Is it for storing return value of function in a variable? Or is this just a way to pass arguments?
#include <iostream>
int ReadNumber()
{
using namespace std;
cout << "Enter a number: ";
int x;
cin >> x;
return x;
}
void WriteAnswer(int x)
{
using namespace std;
cout << "The answer is " << x &开发者_如何学运维lt;< endl;
}
int main()
{
int x = ReadNumber();
int y = ReadNumber();
WriteAnswer(x+y);
return 0;
}
- "why does he assign a function to a variable?"
He doesn't. This(note the brackets):
// vv
int x = ReadNumber();
assigns the returned value to x
. The brackets mean, that the function is actually called - or the body is executed. That function has return x;
at the end, so it returns the value of x
which is assigned to x
- the one in the main. Note that the x
in main
and x
in ReadNumber
are totally different.
Also, you can't assign function to a variable in C++
(you can use function pointers, but this is another thing)
- "What's the point in assigning int x, y to ReadNumber() ?"
The returned value from ReadNumber
is a temp value and it should be stored somewhere. So, that's why x
and y
are defined in the main
, so that each of them stores the value, returned from ReadNumber
. And each of these values can be different.
If, in main
, there were no x
and y
, the returned values are unusable and cannot be accessed at all. And they are destroyed.
- "Is it for storing return value of function in a variable? Or is this just a way to pass arguments?"
No any arguments here. Arguments are written inside the brackets ( ()
) and here, there are no such when calling ReadNumber
. So yes, they are for storing the returned values.
WriteAnswer
does not have return
at the and and it's defined as void
- which means - no return value. That's why there's no such thing as
int x = WriteNumber( X + y )
But note, that here WriteNumber
has argument. Just one, and it's the value of the calculated x + y
. So, it's like:
int z = x + y;
WriteNumber( x );
Yes, it is storing the return value of the function.
The code calls the ReadNumber function twice first. ReadNumber reads a string from the console then returns it as an int. The code stores the two function returns. It then adds these two numbers together and passes it as an argument to the WriteAnswer function. The WriterAnswer function takes the argument that was passed in and prints it out.
int x = ReadNumber();
By this statement, the programmer wants to assign the return value of that function to the variable. If you check the function signature, you can see that it returns a value of type int
.
ReadNumber()
reads a number from the standard input and returns it.
int x = ReadNumber();
stores that value into x
.
Actually the function is not assigned to variable
What happened is each time ReadNumber()
is called, it returns a number of type int
. that number can be used anywhere, like putting in a variable like x
, and we can call it one more time and assign the next value returned to another variable like y
The result of ReadNumber() is assigned to x - not the other way around. ReadNumber() asks the user for a number and assigns it to x, then for another number and assigns it to y. Then main() adds them together and shows the user using WriteAnswer().
He doesn't assign the function/method itself but the results...
He's assigning the results of two successive calls of method: ReadNumber() to the variables x and y respectively.
So the returned value of ReadNumber() is what is assigned.
See the return type of ReadNumber() is int - the same type as the variables being assigned.
I've commented the method below to make it clearer:
int ReadNumber()
{
using namespace std;
cout << "Enter a number: "; // This line prints the command to the standard output
int x;
cin >> x; // This line reads a number and assigns it to x
return x; // This line returns the number and ends the method...
}
精彩评论