0

Have I misunderstood the return line?

I have been using this thinking it passed the result of variable manipulation in a function back to the main code. So if I generate a die roll in a function, I have done something like this: DieRoll () { Die= 1 + rand() %6; return Die; } and if I have not done any variable manipulation, I have used a Void function. Reading back through the lessons on this app, I don't think this is right! So what is the return 0 actually for? And when would I need a Void Function? Thank you.

4th Aug 2017, 4:06 AM
Richard Appleton
Richard Appleton - avatar
2 Respuestas
+ 11
You use a void function when you don't need to return anything back to the caller. void speak() { std::cout << "Something."; } // which can be called in main as speak(); For a dice roll, you will want to return an integer value back to the caller. int roll() { return 1+ rand() % 6; } int main() { std::cout << roll(); return 0; }
4th Aug 2017, 6:01 AM
Hatsy Rei
Hatsy Rei - avatar
+ 5
int count=0; void printOut(what) { printf("Line %d: %s\n", count++, what); // why return a printf status? // also, you can see count outside } By convention, returning 0 means success. *** By the way, mod can unbalance rand() ... just something to know: https://stackoverflow.com/questions/10984974/why-do-people-say-there-is-modulo-bias-when-using-a-random-number-generator
4th Aug 2017, 4:33 AM
Kirk Schafer
Kirk Schafer - avatar