Thread: can I have two returns?

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    33

    can I have two returns?

    can I have an inted function return two values? thanks

  2. #2
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    Functions can only return 1 value... You can return a pointer to a struct with lots of values in it, though.

  3. #3
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802


    int theFunction(int num1, int num2)
    {

    int *ptrReturn = new int[2];
    ptrReturn[0] = (num1 + num2) / 2;
    if (num1 < num2)
    {
    ptrReturn[1] = num2;
    } else {
    ptrReturn[1] = num2;
    }

    return ptrReturn;
    }

    <Meanwhile, outside the function>
    int avgPlusHighest = theFunction(2, 3);
    // avgPlusHighest[0] == Average;
    // avgPlusHighest[1] == Highest number;


    Someone correct me if I'm wrong

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    That works but you have to remeber to free the memory used after you are finished with it. It is often better to pass an extra variable or two in by reference if your function needs to return more than one value.That way the changes to the values in the function actually makes changes to the values in the calling function.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Unregistered
    Guest
    Or just pass an array to the function and let it be filled by the function.

  6. #6
    Registered User
    Join Date
    Feb 2002
    Posts
    33
    thanks all

  7. #7
    Registered User
    Join Date
    Feb 2002
    Posts
    33
    well... here's the function:

    int PythSimp(int i, int one, int two, int a)
    {
    one = one * one;
    two = two * two;
    one = one + two;
    for (i=1;one % i !=0;i++)
    {
    two=one/i;
    }

    return two;
    }

    I want this function to tell me what two is and what i is. is there a way to do this with this type of function?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  2. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  3. Function returns null value instead of zero
    By drdepoy in forum C Programming
    Replies: 3
    Last Post: 10-23-2005, 03:51 PM
  4. Replies: 2
    Last Post: 04-12-2004, 01:37 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM