What happens if the return statement is commented out?
Nothing happens when i removed the return statement in void findNumber function.
Can anyone expalin to me what is difference when putting return statement in void function. With return and without it. I didnt see the differences.
#include <iostream>
#include <cstdlib>
#include <ctime>
usingnamespace std;
constint MAXINDEX = 10000;
int list[MAXINDEX];
void generateNumbers() {
srand((unsigned)time(0)); // seed the random generator
for (int i=0; i < MAXINDEX; i++)
list[i] = (rand() % 100)+1; // generate a random number
}
void findNumber(int numToFind) {
for (int i=0; i < MAXINDEX; i++) {
cout << "Searching...index: " << i << endl;
if (list[i] == numToFind) {
cout << "Found number!" << endl;
return;
}
}
}
int main() {
int number;
generateNumbers();
do {
cout << "Enter number to be searched: ";
cin >> number;
findNumber(number);
} while (true);
system("PAUSE");
return 0;
}Put the code you need help with here.
Using the return statement in a void function is only useful if you want the function to end before the end of the function is reached. In the findNumber function, if you remove the return, it will keep searching the list until it has searched all 10000 elements even if the number was found earlier.