Thread: Question With Functions

  1. #1
    Registered User
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    27

    Arrow Question With Functions

    hi! i have a question with functions!

    i created a function which deals a card, basically what i wanted to do is use the first return value again.

    i dunno how!

    example:
    Code:
    DealCard();
    
    /* codes codes codes */
    
    /*then i want to use/print the value first generated by the DealCard Function */
    i tried
    Code:
    nDCard=DealCard();
    
    /*code code code */
    
    printf ("%c", nDCard);
    but what happened was it dealt another card.

    help me!

    thanx!

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    A guess would be that the problem lies either here
    Code:
    /*codes codes codes */
    or here
    Code:
    /*code code code */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    27
    meaning..

    Code:
    nDCard1=DealCard();
    printf ("%c", nDCard1);
    is a correct syntax for printing the first generated value?

    DealCard() is a function for randomly generating the card disregarding the suit.

  4. #4
    Registered User
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    27
    okay so here is the program flow..(this is a blackjack game)

    1.deals one card to the dealer then prints it
    2. prints concealed as the second card of the dealer should be concealed.
    3. the next codes would be dealing the card for the player so that really doesnt matter
    4. >>>if the player stands, the first value printed should be displayed..<<<
    5. then deals a second card to the dealer
    6. then codes to determine if the dealer is busted or not.

    algo number is my question, how do i do that? cuz with the code above, it randlmly deals another card to the dealer instead of printing the first dealt card..

    please i really need help!

  5. #5
    Registered User
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    27
    someone?? pls pls pls pls

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    First, you shouldn't bump your own posts like that.

    Second, a function is only called as many times as you call it. That's one of the beautiful things about C. There's no behind-the-scenes magic.

    Third, maybe you should post all of the relevant code instead of making everyone guess at what the problem is.
    If you understand what you're doing, you're not learning anything.

  7. #7
    Registered User
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    27
    i'm sorry.

    Code:
    /*Function to Deal a card*/
    
    char DealCard (nDCard)
    { char cCard;
    
    nDCard=rand()%13+1;
    switch (nDCard)
               { case 1: printf ("Ace of");
                         break;
                 case 2: printf ("Two of");
                         break;
                 case 3: printf ("Three of");
                         break;
                 case 4: printf ("Four of");
                         break;
                 case 5: printf ("Five of");
                         break;
                 case 6: printf ("Six of");
                         break;
                 case 7: printf ("Seven of");
                         break;
                 case 8: printf ("Eight of");
                         break;
                 case 9: printf ("Nine of");
                         break;
                 case 10: printf ("Ten of");
                          break;
                 case 11: printf ("Jack of");
                          break;
                 case 12: printf ("Queen of");
                          break;
                 case 13: printf ("King of");
                          break;
               }
    return (cCard);
    }
    Code:
    /*Function to determine card suit*/
    char Suit (nSuit)
    {    char cSuit;
         nSuit=rand()%4+1;
         switch (nSuit)
                 { case 1: printf (" Hearts"); break;
                   case 2: printf (" Diamonds"); break;
                   case 3: printf (" Spades"); break;
                   case 4: printf (" Clubs"); break;
                 }
                 
    return (cSuit);
    }

    Code:
    cCard=DealCard();
    cSuit=Suit();
    printf ("\n");
    printf ("%c", cCard);
    printf ("%c", cSuit);
    DealCard();
    Suit();
    
    /*expected output, assuming the value random generated is Three of Hearts*/
    Three of Hearts
    
    Three of Hearts
    Four of Spades
    Code:
    /*what happens is this, assuming the same value is generated*/
    Three of Hearts
    
    Four of Spades

    i'm sorry again. pls help me and thanx a lot

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You're totally ignoring what you pass to your functions. Look at this one:
    Code:
    char Suit (nSuit)
    {    char cSuit;
         nSuit=rand()%4+1;
         switch (nSuit)
                 { case 1: printf (" Hearts"); break;
                   case 2: printf (" Diamonds"); break;
                   case 3: printf (" Spades"); break;
                   case 4: printf (" Clubs"); break;
                 }
                 
    return (cSuit);
    }
    You pass it a value, but then you overwrite it with your call to rand() right away.
    If you understand what you're doing, you're not learning anything.

  9. #9
    Registered User
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    27
    i didnt quite get what u mean...

  10. #10
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    If you do:
    Code:
    {
      int a = 5;
    
      a = 8;
    }
    Is a still going to be 5?
    If you understand what you're doing, you're not learning anything.

  11. #11
    Registered User
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    27
    nsuit is an integer, the formula that i did was for suit function to generate a number from one to 4, but i dont need the number, what i need is the suit itself so i used switch. what i want to do is transfer the return value to a variable which i can reuse again without it changing value.

    and that i dont have any idea how to do..oh my god i'm so confused
    Last edited by jarvis; 07-10-2006 at 05:45 AM.

  12. #12
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    It has nothing to do with the way you're calling the function. Just look inside the function. You're overwriting what you passed to the function with the call to rand().
    If you understand what you're doing, you're not learning anything.

  13. #13
    Registered User
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    27
    we havent studied functions yet so i really dont know what i'm doing, i just followed the syntax on our book..wait...so u mean ..ah ok now i'm geting it, now what am i suppose to put it inside the formal parameters (which i dunno what it is?) can i leave it blank ? like..

    Code:
    char Suit()
    {   char cSuit;
         int nSuit;
    
         nSuit=rand()%4+1;
         switch (nSuit)
                 { case 1: printf (" Hearts"); break;
                   case 2: printf (" Diamonds"); break;
                   case 3: printf (" Spades"); break;
                   case 4: printf (" Clubs"); break;
                 }
                 
    return (cSuit);
    }
    ?

  14. #14
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    call it like you had: char Suit(int nSuit)

    It has nothing to do with the fact that it's a function. You're simply overwriting the value of your variable without ever using it
    If you understand what you're doing, you're not learning anything.

  15. #15
    Registered User
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    27
    wait so how am i gonna print its value then? then use the function again to deal a card? i really hope i'm not bugging you

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, 07:00 AM
  2. Functions Question
    By audinue in forum C Programming
    Replies: 2
    Last Post: 01-09-2009, 09:39 AM
  3. functions question.
    By Boozel in forum C Programming
    Replies: 1
    Last Post: 02-23-2008, 12:38 AM
  4. Question concerning functions
    By Warrax in forum C++ Programming
    Replies: 5
    Last Post: 04-04-2007, 11:00 AM
  5. Question about creating flash functions
    By jbh in forum C++ Programming
    Replies: 8
    Last Post: 11-21-2005, 09:39 AM