newb question about interpreting and writing functions

This is a discussion on newb question about interpreting and writing functions within the C Programming forums, part of the General Programming Boards category; I'm having some difficulties understanding how functions work in a program. In the case of this function: Code: typedef enum ...

  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,185
    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

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