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; In my main I call a function to compute some things. I send in the raw data to the 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,211
    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,211
    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,185
    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, 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