Function returning more than one value?

This is a discussion on Function returning more than one value? within the C Programming forums, part of the General Programming Boards category; Hi, Is it possible for a single function to return more than one value? Code: void Prompt(void) { double households[SIZE], ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    100

    Function returning more than one value?

    Hi,
    Is it possible for a single function to return more than one value?

    Code:
    void Prompt(void)
    
    {
    	double households[SIZE],
    		   average=0,
    		   maximum,
    		   minimum,
    		   input;
    	
    	int	   incomes; 	
    
    	printf("\nPlease enter the families income.\n");
    
    	for(incomes=0; incomes<SIZE; ++incomes)
    	{
    		printf("\nFamily (%d): " , incomes+1);
    		scanf("%lf" , &input);
    		average += maximum = minimum = input; 
    		households[incomes] = input;
    	}
    				
    return maximum;
    return minimum;     /*like this*/
    return average;
    }
    If so, how would the returns be coded in main()?

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    17
    Pointers are your friends...
    Code:
    #include <stdio.h>
    
    int foobar (int *n)
    {
        *n = 10;
        return 5;
    }
    
    int main(void)
    {
        int a;
        int b;
    
        a = foobar(&b);
    
        printf("a: %d\nb: %d\n", a, b);
        return 0;
    }

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    100
    Unfortunately I don't know pointers yet. So I guess a function alone can't do it?

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    17
    I gave you a very simple example: understand and use it.

    The answer to your question is no.

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Define a structure and use that to return the values.

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    100
    Don't know how to do that yet either.

  7. #7
    chococoder
    Join Date
    Nov 2004
    Posts
    515
    then learn it...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem in returning value from the dll exported function
    By dattaforit in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2006, 04:30 AM
  2. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  3. returning pointers from a function
    By curlious in forum C++ Programming
    Replies: 2
    Last Post: 12-28-2003, 11:37 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM

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