Originally Posted by
utherson601
i was wondering how to modify the following to make the loop end without using "return".
in otherwords im asking how to use "meow()" to directly edit "main()"s int1
Code:
void meow(int int1)
{
int1 += 10;
cout << int1 << endl;
}
int main()
{
int int1 = 5;
while(int1 < 100)
{
meow(int1);
}
system("pause");
return 0;
}
Use a reference.
Code:
void meow(int& int1)
{
int1 += 10;
cout << int1 << endl;
}
int main()
{
int int1 = 5;
while(int1 < 100)
{
meow(int1);
}
system("pause");
return 0;
}
You can also use a pointer (less desired method in C++, but only method in C):
Code:
void meow(int* int1)
{
*int1 += 10;
cout << *int1 << endl;
}
int main()
{
int int1 = 5;
while(int1 < 100)
{
meow(&int1);
}
system("pause");
return 0;
}
However, this deafeat the purpose of a function, which is to be more or less self-contained. Rarely should you use references in this sort of context.