Like Tree4Likes
  • 2 Post By iMalc
  • 2 Post By laserlight

New to Functions

This is a discussion on New to Functions within the C++ Programming forums, part of the General Programming Boards category; I have this code and I want to be able to call it as a function. I looked up how, ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    7

    Question New to Functions

    I have this code and I want to be able to call it as a function. I looked up how, but am pretty confused and always get an error. Here is my code I want to make a function without global variables:

    Code:
    srand(time(0));  //seed the random number
    for(int i=1; i<=12; i++ ) //12 iterations 
    {
      randomizer = rand() % 2;
      
      if (randomizer == 0)
      {
    	  
    	selection += ( selection == 8  ) ?  -0.5 : 0.5;
      } 
      else
      {
    	selection += ( selection == 0  ) ?  0.5 : -0.5;
      }
    
    
      cout << selection << endl; //print out the 12
    }
    Last edited by skittlesaddict; 10-07-2012 at 06:18 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    What did you try and what was the error?
    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
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Note that if you turn this into a function you do not want to include the srand line. That line should only be executed once in your program.
    rags_to_riches and Elysia like this.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Or to make it more elegant in large programs,use srand inside the function by doing this
    Code:
    // inside the function
            static int Init = 0;
            
    
            if (Init == 0)
            {
                     /*
                      *  As Init is static, it will remember it's value between
                      *  function calls.  We only want srand() run once, so this
                      *  is a simple way to ensure that happens.
                      */
                     srand(time(NULL));
                     Init = 1;
             }
             ......
    // end of function

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    Quote Originally Posted by std10093
    Or to make it more elegant in large programs,use srand inside the function by doing this
    I don't think that is a good idea. Just write the function without calling srand. srand should then be called near the start of the main function. This way, it becomes easier if you want to swap out the specific use of rand for some other PRNG with a similiar interface (or even change to use the C++11 PRNG library), and you don't have to worry that some function that calls rand fails to check Init in order to call srand before use.
    Elysia and iMalc like this.
    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

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    7
    Here is my full program and what I tried.
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std; 
    
    
    void myFunction(double randomizer, double selection)
    {
       for(int i=1; i<=12; i++) //12 iterations starting from 1
       {
            randomizer = rand() % 2;
            if (randomizer == 0)
            {
               selection += ( selection == 8  ) ?  -0.5 : 0.5;
         	} 
            else
            {
               selection += ( selection == 0  ) ?  0.5 : -0.5; 	
            }
            cout << selection << endl; //print out the 12 iterations
       }
    }
    int myOtherFunction(int selection, int user_winnings)
    {
    if (selection == 0){
    	user_winnings = 100;
    }
    if (selection == 1){
    	user_winnings = 500;
    }
    if (selection == 2){
    	user_winnings = 1000;
    }
    if (selection == 3){
    	user_winnings = 0;
    }
    if (selection == 4){
    	user_winnings = 10000;
    }
    if (selection == 5){
    	user_winnings = 0;
    }
    if (selection == 6){
    	user_winnings = 1000;
    }
    if (selection == 7){
    	user_winnings = 500;
    }
    if (selection == 8){
    	user_winnings = 100;
    	return user_winnings;
    }
    }
    int main()
    {
    //set variables
    int choice;
    double selection;
    int where_drop = 0;
    double user_winnings;
    int randomizer;
    int multiple_chips;
    double selection_two;
    int randomizer_two;
    double user_winnings_two;
    double average_winnings;
    bool gameOn = true;
    
    
    //make menu screen
    while (gameOn != false){
    cout << "***GAME MENU***\n";
    cout << " 1 - Drop one chip into one slot.\n";
    cout << " 2 - Drop multiple chips into one slot.\n";
    cout << " 3 - Drop Multiple chips into every slot.\n";
    cout << " 4 - Exit.\n";
    cin >> choice;
    
    
    //menu choices below in switch
    switch (choice)
    {
    case 1:
    	
    	cout << "\n Which Slot would you like to Drop a chip in(Please enter a slot #0-8)?"<<"\n";
    	cin >> selection;
    
    
    if((selection < 0) || (selection > 8))//if a negative number or greater than 8 go back to menu
    {
    	break;
    }
    
    
    srand(time(0));  //seed the random number
    
    
    void myFunction();
    
    
    //find winnings
    int myOtherFunction();
    //print out winnings
    	cout<<"You've won $" << user_winnings << "!" << endl;
    
    
    break;
    case 2: //second option
    
    
    cout << "\n How many chips would you like to play?"<<"\n";
    cin >> multiple_chips;
    
    
    if(multiple_chips < 0)//don't allow negative numbers
    {
    	break;
    }
    
    
    cout << "\n Which Slots would you like to Drop the chip(s) into(Please enter a slot #0-8)?"<<"\n";
    cin >> selection;
    
    
    if((selection < 0) || (selection > 8))//only allow 0-8 as option or return to menu
    {
    	break;
    }
    
    
    void myFunction();
    
    
    //find winnings. 
    int myOtherFunction();
    
    
    //final calculations
    average_winnings = user_winnings / multiple_chips;
    
    
    	cout<<"You've won  a total of $" << user_winnings << "!" << endl;
    	cout<<"You averaged winnings of $" << average_winnings << " a chip." << endl;
    
    
    break;
    case 3:
    cout << "New stuff.\n";//working on finishing
    break;
    case 4://quit program
    cout << "End of Program.\n";
    gameOn = false;
    break;
    default:
    cout << "Not a Valid Choice. \n";
    cout << "Choose again.\n";
    break;
    cin >> choice;
    
    
    }
    
    
    }
    return 0;
    }

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Turn up your compiler warning level and pay attention to the warnings.
    Your myOtherFunction function is not returning a value for most of the code paths because you have the return statement in the wrong place.
    In any case, you should be using a switch statement or lookup table intead of lots of if-statements. You should also not be passing user_winnings in as a parameter, in fact you don't even need that variable as a local either as you can just return the value directly.
    Also consider what value you will return when none of the cases are matched. Hint: I would eliminate cases 3 and 5 if using a switch statement, and leave them to be handled by the default.

    That's all just stuff that should be changed inside one function. Other things are very wrong too, such as wrong unnecessary and misplaced function prototypes, and bad indentation.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WinAPI functions - similar functions as part of VS C++
    By jlewand in forum Windows Programming
    Replies: 2
    Last Post: 02-02-2012, 08:54 AM
  2. Creating Functions & passing information to other functions
    By RyanLeonard in forum C Programming
    Replies: 4
    Last Post: 10-28-2010, 01:17 PM
  3. Replies: 7
    Last Post: 04-19-2006, 12:17 PM
  4. Replies: 6
    Last Post: 05-06-2003, 04:08 PM
  5. Replies: 1
    Last Post: 01-20-2002, 11:50 AM

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