Click to See Complete Forum and Search --> : Help with functions


MasterDucky
March 11th, 2008, 10:52 AM
Sorry for the noob question but i still dont understand the benefits of a
function that does something but returns nothing.
Whats the point?

void echoSquare()
{
int value;
cout << “Enter a value:”;
cin >> value;
cout << “\n The square is:” << (value * value) << “\n”;
return;
}

GCDEF
March 11th, 2008, 11:06 AM
It compartmentalizes your code to reduce clutter and allows you to call the same code from different parts of your program without having to retype it.

Also useful in object oriented programming, where each object will be operating on its own data.

MasterDucky
March 11th, 2008, 11:31 AM
I think i didnt put it right.
The question is not what's the use of functions.

I asked whats the use of functions that returns nothing.
You have functions that return a value and functions that return
nothing at all.
I dont see what good are they for.

GCDEF
March 11th, 2008, 11:34 AM
I think i didnt put it right.
The question is not what's the use of functions.

I asked whats the use of functions that returns nothing.
You have functions that return a value and functions that return
nothing at all.
I dont see what good are they for.

My answer has nothing to do with what they return.

Lindley
March 11th, 2008, 11:37 AM
Side-effects. Sometimes side-effects include printing something to the console; in the case of class member methods, they may modify class state. Sometimes they may change the value of a global variable, although this usage is discouraged most of the time.

Also, sometimes values are "returned" simply by modifying parameters passed by reference (or pointer parameters), rather than via explicit returns.

MrViggy
March 11th, 2008, 11:45 AM
To add to what Lindley and GCDEF said, a member function of a class may not return anything, but might modify the state of the object that it is operating on.

Viggy

PS. D'OH! I skipped that phrase in Lindley's response! Sorry!!! ;)

MasterDucky
March 11th, 2008, 12:10 PM
Ah ok i see, i wonder why i didnt see this.
I guess i got stuck on the fact that if it does something it has to give something...
...but it does give something...

Anyway, thanks for the help!