Thread: Returning Multiple Values from a Function

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    12

    Returning Multiple Values from a Function

    In my main I call a function to compute some things. I send in the raw data to the function.

    My function then computes, averages, standard deviations etc.

    I then write my return statement in the function as follows:

    return(average, standardDeviation, highestNumber)

    Now, when I return this to my main, how can I pick out each individual computation and store it to an appropriate variable.

    I want to then pick out the data and store it as follows:

    VariableOne = average;
    VariableTwo = standardDeviation;

    etc...

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    You can't return that way. Multiple returns are illegal in C. Return a struct or pass in the values by reference.

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    12
    Is it possible to return an array?

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Not a local one, no. You can't return an address of a local variable since local variables have the potential to be destroyed around the time as the function returns and control is given to the calling function.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    And to that point, it's not legal to have an array as a return type, anyway, only a pointer. The usual method is to pass the array in as a parameter and write to it.

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, 02:28 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Replies: 1
    Last Post: 02-03-2005, 03:33 AM
  5. c function returning multiple values
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 11-23-2001, 10:09 AM