Thread: newb question about interpreting and writing functions

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    43

    newb question about interpreting and writing functions

    I'm having some difficulties understanding how functions work in a program. In the case of this function:

    Code:
    typedef enum {lost, won, go_on} status;
    //more functions
    
    status turnStatus (int rollNum, int total, int pt)
    // function: turnStatus
    // pre: rollNum is 1 or more, total is valid (2 to 12)
    // post: returns the status of the game (won, lost, or go_on)
    {
       if (rollNum == 1) // first roll of the dice
          if (total == 2 || total == 3 || total == 12)
             return lost;
          else if (total == 7 || total == 11)
             return won;
          else
             return go_on;
       else
          if (total == 7)
             return lost;
          else if (total == pt)
             return won;
          else
             return go_on;
    }
    How are the variables actually defined? What makes up "total", etc? I see checks for equality, but Im used to seeing something like total = (x * y) or something like that to indicate exactly where total came from. Is this tied in with the precondition somehow? Also, in this particular program, the main does not declare these variables. they only are used here.

    Any info is appreciated
    Thanks,
    crazychile

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    total gets passed into the function. You'll have to look at the calling code to find out what it makes.

Popular pages Recent additions subscribe to a feed