Question concerning functions

This is a discussion on Question concerning functions within the C++ Programming forums, part of the General Programming Boards category; I'm currently on the functions tutorial, but there's one thing that I just can't understand how to do (which it ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    23

    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.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,888
    Have you checked out loops yet?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,338
    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;
    }

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    23
    K, thanks. Yeah, I've done the loops tutorial, I just forgot it already >.<.

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    23
    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).

    The code I'm confused about:

    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;
    }
    Any idea why it won't loop?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,888
    x is your look index, but you also use it in your loop. Use some other variable for the loop index.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner's question about functions.
    By Crocodile23 in forum C Programming
    Replies: 4
    Last Post: 01-13-2009, 06:00 AM
  2. Functions Question
    By audinue in forum C Programming
    Replies: 2
    Last Post: 01-09-2009, 08:39 AM
  3. functions question.
    By Boozel in forum C Programming
    Replies: 1
    Last Post: 02-22-2008, 11:38 PM
  4. Question about functions
    By richdb in forum C Programming
    Replies: 2
    Last Post: 01-22-2006, 12:18 AM
  5. Question about creating flash functions
    By jbh in forum C++ Programming
    Replies: 8
    Last Post: 11-21-2005, 08:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21