Thread: how to return 2 values upon calling a function

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    116

    how to return 2 values upon calling a function

    Hi everyone. How would i return two values each time I call a function? I have this program here, that is extracting 2 values from a text file every time a function is called, but I only seem to be able to return one value each time the function is called. I need to return column1 and column2 each time readingFunction() is called and they should be stored in readingsArray[loop] and temperatureArray[loop]. is it possible to return two values upon calling a function?

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    float readingFunction(int loop);
    
    
    int main(void)
    {				
    	int menu = 0;							 							  
    	int readingsArray[10];
    	float temperatureArray[10];
    	int loop=0;
    
    
    	do
    	{
    		printf("\n\n The sensor readings and their corresponding temperatures:\n\n");
    
    
    		for(loop=0; loop < 10; ++loop) // Gives the user a set of readings
    		{
    			temperatureArray[loop] = readingFunction(loop); 
    			printf("\nreading number %i is %i which is %f degree's", loop, temperatureArray[loop]);
    		}
    
    
    		
    	return 0;
    
    
    
    
    float readingFunction(void)
    { 
    	FILE *fp;	
    	int signalOutput;
    	int flag=0;
    	int column1;
    	float column2;
    	srand(time(NULL));	
    
    
    	fp = fopen("LOOKUP.txt", "r");
    	signalOutput = (int)(rand()%15);
    	flag=0;
    			
    
    
    	while( ! flag ){
    		fscanf(fp, "%i %f",&column1, &column2);
    				
    		if ( signalOutput <= column1 ) 
    			{
    					flag = 1;
    					fclose(fp);			
    			}	
    		}
    
    
    	return column1;
    }

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    in C you can only return a single value from a function as the return value. however the return value can be a struct. so one way to do it would be to declare a struct with the 2 elements you need and return the entire struct.
    Code:
    struct X {
        float a;
        float b;
    };
    
    struct X function(void) {
       struct X r;
       r.a = 0.0f;
       r.b = 1.0f;
       return r;
    }
    
    ...
    struct X x;
    x = function();
    printf("%f %f\n",x.a,x.b);
    the other way is to pass in pointers to the return values you want as arguments and set the values thru the pointers
    Code:
    void function(float *a,float *b)
    {
        *a = 0.0;
        *b = 0.0;
       // no return statement needed
    }
    
    ...
    float x;
    float y;
    
    function(&x,&y);
    
    printf("%f %f\n",x,y);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-18-2011, 12:13 AM
  2. How to properly return MySQL results to a calling function?
    By Effenberg0x0 in forum C Programming
    Replies: 3
    Last Post: 06-09-2011, 03:42 AM
  3. Having a function return three values
    By alanb in forum C++ Programming
    Replies: 12
    Last Post: 09-02-2009, 08:51 AM
  4. function return values
    By ashok449 in forum C Programming
    Replies: 10
    Last Post: 12-21-2007, 02:57 AM
  5. can a function return 2 values?
    By panfilero in forum C Programming
    Replies: 2
    Last Post: 09-27-2005, 02:35 AM