Question concerning functions

• 04-04-2007
Warrax
Question concerning functions
I'm currently on the functions tutorial, but there's one thing that I just can't understand how to do (which it doesn't explain how, but says that it's possible and a good idea to use). How would I go about making a function to actually repeat a certain block of code a certain amount of times? The only type of functions that the tutorial explains is a function that will accept two arguments, and return an integer. Also note that I'm not asking how to do this for a specific program; I just need to know how I'd go about doing this.

Also, if possible, please write out both in code and explaining it to me so I can look at the structure of the code if I don't understand something.
• 04-04-2007
laserlight
Have you checked out loops yet?
• 04-04-2007
Daved
You just call the function multiple times:
Code:

```#include <iostream> using namespace std; int mult ( int x, int y ); int main() {   int x;   int y;     cout<<"Please input two numbers to be multiplied: ";   cin>> x >> y;   cin.ignore();   cout<<"The product of your two numbers is "<< mult ( x, y ) <<"\n";   cout<<"Please input two numbers to be multiplied: ";   cin>> x >> y;   cin.ignore();   cout<<"The product of your two numbers is "<< mult ( x, y ) <<"\n";   cout<<"Please input two numbers to be multiplied: ";   cin>> x >> y;   cin.ignore();   cout<<"The product of your two numbers is "<< mult ( x, y ) <<"\n";   cin.get(); } int mult ( int x, int y ) {   return x * y; }```
or, if you know for loops:
Code:

```#include <iostream> using namespace std; int mult ( int x, int y ); int main() {   int x;   int y;   for (int i = 0; i < 40; ++i)   {      cout<<"Please input two numbers to be multiplied: ";     cin>> x >> y;     cin.ignore();     cout<<"The product of your two numbers is "<< mult ( x, y ) <<"\n";     cin.get();   } } int mult ( int x, int y ) {   return x * y; }```
• 04-04-2007
Warrax
K, thanks. Yeah, I've done the loops tutorial, I just forgot it already >.<.
• 04-04-2007
Warrax
Kind of confused though; when I typed up that code it won't loop regardless of the fact that it looks almost exactly like yours (the only differences are spaces between code).

```#include <iostream> using namespace std; int mult ( int x, int y ); int main() {     int x;     int y;     for ( int x=0; x < 73; x++ )     {     cout<<"Please input two numbers to be multiplied.\n";     cin>> x >> y;     cin.ignore();     cout<<"The product of your two numbers is "<< mult ( x, y ) <<"\n";     cin.get();     } } int mult ( int x, int y ) {           return x * y; }```