2 Replies - 1216 Views - Last Post: 14 March 2013 - 06:01 AM Rate Topic: -----

#1 OldManofTheClass   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 28-February 13

Using C trying to return multiple values from a Function

Posted 13 March 2013 - 10:40 PM

Can you get multiple values from C functions?? All I get is zeros.
I have some stubs in so I know the values are populated properly The function just doesn't return them.

The idea is that this function will deal one card to a player. I need the rank, suite and card value
returned to display and add to the total for each hand to determine the winner.

I am carving this out of a previous assignment so it may be a little confusing. But I need to know if
I can even get the values out of the function before I go further with this tac.
THANKS FOR ANY HELP :)

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define NUM_SUITE 4
#define NUM_RANKS 13
#define IPLAYERS 4
#define NUM_CARDS 10

int DealCard()
{

   char player_suite[IPLAYERS][NUM_CARDS] = {' '};
   char player_rank[IPLAYERS][NUM_CARDS] = {' '};
   int num_cards=1;
   int num_players=1;
   int rank;
   int suite;
   int cardValue=0;

   const char rank_code[] = {'2', '3', '4', '5', '6', '7', '8',
                             '9', 'T', 'J', 'Q', 'K', 'A'};
   const char suite_code[] = {'c', 'd', 'h', 's'};

   srand((unsigned) time(NULL));
   
	for(int i = 0; i < num_players; i++)
	{
     suite = rand() % NUM_SUITE;  // picks a random suit
     rank = rand() % NUM_RANKS;   // picks a random rank 

     
		player_suite[i][num_cards] = suite_code[suite];
		player_rank[i][num_cards] = rank_code[rank];
		if (rank > 7 )
			cardValue += 10;
		else
			cardValue += rank + 2;

        printf(" %c    %c\n", player_rank[i][num_cards], player_suite[i][num_cards]);//STUB

		printf("player total = %d\n", cardValue);//STUB
	} // end of for loop
	num_cards--; DECREMENT SO LOOP ENDS
	
   return rank, suite, cardValue;
} //end DealCard

int main(void)
{

printf("Card Rank : %d, Card Suite: %d, Card Value: %d\n",DealCard());  //THIS IS WHERE I DO NOT GET ALL 3 VALUES BACK

}





Is This A Good Question/Topic? 0
  • +

Replies To: Using C trying to return multiple values from a Function

#2 jimblumberg   User is offline

  • member icon

Reputation: 5790
  • View blog
  • Posts: 17,705
  • Joined: 25-December 09

Re: Using C trying to return multiple values from a Function

Posted 13 March 2013 - 10:48 PM

You can only return one variable from a function. You may however use parameters to your function to "return" values to the calling function. You may want to study the function tutorials contained in my signature below. You may also want to consider using a structure/class to hold the information about your cards.

Jim

This post has been edited by jimblumberg: 13 March 2013 - 10:49 PM

Was This Post Helpful? 1
  • +
  • -

#3 stackoverflow   User is offline

  • D.I.C Addict
  • member icon

Reputation: 166
  • View blog
  • Posts: 545
  • Joined: 06-July 11

Re: Using C trying to return multiple values from a Function

Posted 14 March 2013 - 06:01 AM

You can return multiple values if you wrap them in something.

typedef struct Point {
	int x;
	int y;
} Point;

Point get_point(int x, int y) {
	Point p = { x, y };
	return p;
}



Replace Point with whatever wrapper you want.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1