Returning Multiple Values from a function

This is a discussion on Returning Multiple Values from a function within the C Programming forums, part of the General Programming Boards category; I have two arrays, x and y, within a function. I want to use these arrays in another function but ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Location
    London
    Posts
    7

    Returning Multiple Values from a function

    I have two arrays, x and y, within a function.

    I want to use these arrays in another function but as I understand it any function can only return 1 value.

    How do I do it?!?!?!?!

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    you can pass pointers to objects and let the function fill in the data

    Code:
    void foo(int* a, int* b)
    {
       *a = 1;
       *b = 2;
    }
    
    int main()
    {
       int a, b;
      foo(&a, &b);
    }

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Or you could just search the forum, since this exact topic (and more than likely exact same thread title) get posted every single week.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Mar 2006
    Location
    London
    Posts
    7
    Quote Originally Posted by Ancient Dragon
    you can pass pointers to objects and let the function fill in the data

    Code:
    void foo(int* a, int* b)
    {
       *a = 1;
       *b = 2;
    }
    
    int main()
    {
       int a, b;
      foo(&a, &b);
    }

    Don't really understand that - I'm rubbish, I know.

    Say my code is of the form (at the moment):

    Code:
    void function_name();
    {
    BLAH BLAH BLAH
    BLAH BLAH BLAH
    BLAH BLAH BLAH
    
    while(i < MAX_SIZE) // Checks that i will fit into the array space.
        {
            fs_ret = fscanf(fp,"%f%f", &x[i] , &y[i] );
            
            // The above reads data from the file.	
            // 2 floating point numbers are read, x and y.
            // Loop continues while 2 data pairs are read and i < 128.          		
    		
            if( fs_ret != 2)
                break;   
                
            printf("%8.3f\t%8.3f\n",x[i],y[i]); // Prints the data for user.                   
            
            i++; // Increments the number of pairs read.
    
    BLAH BLAH BLAH
    BLAH BLAH BLAH
    BLAH BLAH BLAH
    Can you explain to me, without just writing code down, I need to understand please, how to get at these values x[i], y[i] so I can use all of their values later on in the program. Thank you.

  5. #5
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    arrays are passed by reference, so when you pass an array to a
    function and manipulate its elements, it manipulates the elements
    of the original arrays. take this:

    Code:
    #define SIZE 10
    
    void call_the_function (int x [], int y []); /*function takes 2 arrays*/
    
    int main (void)
    {
           int arrayX [SIZE];
           int arrayY [SIZE];
           .
           .
           .
           .
           call_the_function (arrayX, arrayY); /*passing arrays to function*/
           .
           . /*you could print arrayX and arrayY and you'll see that they were modified by the function*/
           .
           return 0;
    }
    
    void call_the_function (int x [], int y [])
    {
           .
           .
           .
           while (i < SIZE) /*here, you manipulate the arrays locally as x & y, but you actually manipulate the parameter arrays, arrayX and arrayY*/
           {
                  .
                  .
                  .
           }
    }
    Last edited by Richie T; 03-12-2006 at 09:01 AM. Reason: typo
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  6. #6
    Registered User
    Join Date
    Mar 2006
    Location
    London
    Posts
    7
    Hi Richie - Thanks for the advice. I tried it but couldn't get it to work at all, even though I now think I understand what I'm meant to be doing. As such, I've posted my whole code as it stands and would really appreciate it if you could look at it and see how I can change it to get the annoying x[] and y[] values out of the "read_data_from_file" function!!!

    Arrrghhh!!

    Cheers!

    Code:
    /**********************************************************************************/
    /* Description: This program will read x and y data values from a given text file */
    /* and will calculate and plot the best straight line fit for these data.         */
    /* Author: Edward King                                                            */
    /* Group: Yc                                                                      */
    /* Date: 10th March 2006                                                          */
    /**********************************************************************************/
    
    
    #include <stdio.h>
    #include <math.h>
    #include <grx20.h>
    
    #define MAX_SIZE 128
    				
    float x[MAX_SIZE];
    float y[MAX_SIZE];
    
    int read_data_from_file(void);
    
    #define NO_FILE -1
    
    int main(int argc, char *argv[])
    {   
        
        
        int read_data_from_file(void)
        
        
        /******************************************************************************/
        /* Function Name: read_data_from_file                                         */
        /* Description: This function will read x and y data values from a text file. */
        /******************************************************************************/
        
        { 
        // OPENS A FILE:
        
        int i;	
    
        FILE *fp;
        char file_name[MAX_SIZE]; // Character array for the data filename.
    
        int fs_ret;
        
        // Asks the user to enter the name and location of the data file.
        printf("Please enter location and filename of the datafile: ");
        scanf("%s",file_name);
        printf("\n"); 
        
        fp = fopen(file_name, "r"); // Opens the file.
    
        if(fp == NULL) // Checks for error in opening file.
        {
            printf("\nUnable to open file '%s'.\nCheck directory location and filename are correct.\n"
                     "Please close this program and start again.", file_name);
            return NO_FILE; // Stops program if there is an error in opening the file.
        }
        
        // READS DATA PAIRS FROM THAT FILE:
        
        i = 0; // Initialises data pair count.
    
        while(i < MAX_SIZE) // Checks that i will fit into the array space.
        {
            fs_ret = fscanf(fp,"%f%f",&x[i],&y[i]);
            
            // The above reads data from the file.	
            // 2 floating point numbers are read, x and y.
            // Loop continues while 2 data pairs are read and i < 128.          		
    		
            if( fs_ret != 2)
                break;   
                
            printf("%8.3f\t%8.3f\n",x[i],y[i]); // Prints the data for user.                   
           
            i++; // Increments the number of pairs read.     
        
        } // End loop.  
        
        fclose(fp); // Closes the file.
     
        return i;  // Retains the number of pairs read.
         
        } // END OF "read_data_from_file" FUNCTION.
        
        
          
               
            int no_of_poi_read;
            
            // USE FUNCTION "read_data_from_file" to return number of pairs read.
            
            no_of_poi_read = read_data_from_file();
            
            if( no_of_poi_read <1)
            {
                printf("\n\nNO DATA READ.\n\n"); //Defines error message.
            }
            else
            {
                 //Prints number of pairs read.
                 printf("\n %d data pairs read from the file. \n\n", no_of_poi_read);
            } // End IF statement.      
               
            
    system ("PAUSE");
    } // END OF MAIN

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    you have defined function read_data_from_file() within function main() -- C does not allow nested functions. Move that function from main()
    Code:
    int main()
    {
      // blabla
    }
    
    int read_data_from_file(...)
    {
      // blabla
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  4. Replies: 1
    Last Post: 02-03-2005, 02:33 AM
  5. c function returning multiple values
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 11-23-2001, 09:09 AM

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