2

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 << endl;
}

int main()
{
    int x = ReadNumber();
    int y = ReadNumber();
    WriteAnswer(x+y);
    return 0;
}
phant0m
  • 16,595
  • 5
  • 50
  • 82
shyrabbit
  • 33
  • 1
  • 3
  • 1
    He's assigning the **return** value of the function to those variables. Go read a C++ book, please. – Seb Holzapfel Aug 04 '11 at 10:20
  • Hi shyrabbit. Welcome to SO. You might find the [following thread](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) helpful. It contains a wealth of resources that will help you in learning and understanding the language. Good luck. – Bart Aug 04 '11 at 10:22
  • 3
    Why negative votes for the question? Does that mean, SO does not accommodate newbies? – cppcoder Aug 04 '11 at 10:23
  • 1
    Like I said, I've just started learning C++ yesterday. I figured that out, but I just wanted to make sure I understood the code correctly. – shyrabbit Aug 04 '11 at 10:23
  • @cppcoder Questions from beginners are certainly welcome. There are just some that don't like (very basic) questions being asked which could have been answered by reading a book or tutorial on a subject. I can understand that (and that's why I posted the link in my comment), but it's not something I would downvote for. – Bart Aug 04 '11 at 10:29
  • 1
    There are some trivial things, which may not be understood by reading a book. I think we should not shove away the newbies. – cppcoder Aug 04 '11 at 11:27

7 Answers7

3
  • "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 );
Kiril Kirov
  • 37,467
  • 22
  • 115
  • 187
2

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.

Mike Kwan
  • 24,123
  • 12
  • 63
  • 96
0
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 .

cppcoder
  • 22,227
  • 6
  • 56
  • 81
0

ReadNumber() reads a number from the standard input and returns it.

int x = ReadNumber();

stores that value into x.

Bertrand Marron
  • 21,501
  • 8
  • 58
  • 94
0

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...
}
Rich
  • 3,781
  • 5
  • 34
  • 56
0

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

Kamyar Infinity
  • 2,711
  • 1
  • 21
  • 32
0

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().