C programing, Function return question.

Discussion in 'OT Technology' started by Disguy, Apr 2, 2007.

  1. Disguy

    Disguy Batman > Moon Knight OT Supporter

    Joined:
    Oct 26, 2004
    Messages:
    1,061
    Likes Received:
    0
    In regular C, Can I return multiple values from a function or just one?

    int main()
    yada, yada

    a = func2(A); //a is the returned value
    ___
    int func2 (int A)

    yada, yada..

    return a;//only returns a
    ___

    this works of course.

    but what do i do if i want to return all three varibles.

    ie

    return (a, b, c); //returns the three values.
    ___
    but how do i write it in the beginning.
     
  2. P07r0457

    P07r0457 New Member

    Joined:
    Sep 20, 2004
    Messages:
    28,491
    Likes Received:
    0
    Location:
    Southern Oregon
    return an array or object.

    you can also pass multiple parameters to the function and have the function modify those parameters -- which can net a similar effect.
     
  3. Disguy

    Disguy Batman > Moon Knight OT Supporter

    Joined:
    Oct 26, 2004
    Messages:
    1,061
    Likes Received:
    0
    i can do it

    a = funcA(a);
    b = funcB(b);
    c = funcB(c);

    but i want to do it in one function.
     
  4. P07r0457

    P07r0457 New Member

    Joined:
    Sep 20, 2004
    Messages:
    28,491
    Likes Received:
    0
    Location:
    Southern Oregon
    you can pass func(a,b,c) and pass them byref and have the func directly modify the variables. Otherwise, return an array if it's the same data type, or an object if they're not.
     
  5. Disguy

    Disguy Batman > Moon Knight OT Supporter

    Joined:
    Oct 26, 2004
    Messages:
    1,061
    Likes Received:
    0
    testArray[]=func2(a,b,c);

    a=testArray[0];
    b=testArray[1];
    c=testArray[2];

    ---------------------
    int testArray[2];

    testArray [0] = a;
    testArray [1] = b;
    testArray [2] = c;

    return testArray;
    --------------------
     
    Last edited: Apr 2, 2007
  6. Penguin Man

    Penguin Man Protect Your Digital Liberties

    Joined:
    Apr 27, 2002
    Messages:
    21,696
    Likes Received:
    0
    Location:
    Edmonton, AB
    There are no objects in C, and you can only return small types. Perhaps you mean a pointer to an array or a pointer to a structure? While it is done, I usually consider returning a pointer to memory allocated in the returning function ugly. I've done it, but I prefer not to in general.

    Yes, this is my preferred way of doing things.
     
  7. P07r0457

    P07r0457 New Member

    Joined:
    Sep 20, 2004
    Messages:
    28,491
    Likes Received:
    0
    Location:
    Southern Oregon
    I assumed he was also talking C++.
     
  8. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    C has structs that can be used as a return type.

    Code:
    typedef struct
    {
         int a;
         int b;
         int c;
    } TESTSTRUCT;
    
    TESTSTRUCT func()
    {
         TESTSTRUCT ret;
         ret.a = 1;
         ret.b = 2;
         ret.c = 3;
         return ret;
    }
    
     
  9. Penguin Man

    Penguin Man Protect Your Digital Liberties

    Joined:
    Apr 27, 2002
    Messages:
    21,696
    Likes Received:
    0
    Location:
    Edmonton, AB
    Hm, so they can. I don't think I've ever written functions that return structs, always pointers to structs. Just seems cleaner to me to return pointers.

    He said "regular C" right at the beginning of his post, so I'm assuming he means real, -Wall -ansi C.
     
  10. kilamasta

    kilamasta New Member

    Joined:
    Aug 14, 2006
    Messages:
    108
    Likes Received:
    0
    "In regular C, Can I return multiple values from a function or just one?"

    No you can only RETURN 1 value. Although, there are others way, called indirection, used with pointers.
    int division(int a, int b, int *reminder)
    {
    *reminder = a%b;
    return a/b;
    }

    int main()
    {
    int a=15;
    int b=7;
    int reminder;

    printf("%d",division(a,b,&reminder));
    printf(" r:%d",reminder);
    getchar();
    return 0;
    }

    You work by changing the VALUE POINTED BY *reminder in the fonction. Don't worry your teacher will teach you that, most important thing in c :).

    (Although if you don't understand well after a while, BE WORRIED!!)
     
  11. Bruticus

    Bruticus half dead OT Supporter

    Joined:
    Apr 10, 2004
    Messages:
    4,608
    Likes Received:
    0
    Location:
    Melbourne
    The easiest way for you would be to pass pointers and actually not return anything. This is assuming they are just simple things (chars or ints for example).

    Do some reading up on pointers or arrays if you'd rather go that way. I think structs are probably a bit beyond you at the moment if you are still wondering about returns.
     
  12. StevesVR4

    StevesVR4 Get Arrested

    Joined:
    Jul 1, 2003
    Messages:
    7,328
    Likes Received:
    0
    Am I reading this correctly? Are you suggesting returning a pointer to a local variable in a function? Like in this example?
    Code:
    int *func()
    {
        int a = 5;
        return &a;
    }
    
    If that is indeed what you are talking about, not only is it not preferred, it can get you into trouble. Variable a is allocated in the call stack and when you exit the function, the call stack entry for the function becomes available for reuse by the system. You basically have a pointer to nothing. Sure the variable you set will be available for a short period of time, but at any time the memory your pointer is pointing to can be changed and you will lose your variable. Returning a pointer to a local variable is bad programming practice and can lead to some undefined behavior (including core dumps).
     
  13. Peyomp

    Peyomp New Member

    Joined:
    Jan 11, 2002
    Messages:
    14,017
    Likes Received:
    0
    Return a pointer to an array. This is not complex. You allocate a new variable to hold the return, so there is no risk of it getting lost.
     
  14. Penguin Man

    Penguin Man Protect Your Digital Liberties

    Joined:
    Apr 27, 2002
    Messages:
    21,696
    Likes Received:
    0
    Location:
    Edmonton, AB
    Nope, that's not what I was talking about. I'm talking about pointers to dynamicaly-allocated large types:

    Code:
    struct mystruct *func() {
        struct mystruct *toret = calloc(1, sizeof(struct mystruct));
        toret->myvar = 2;
        return(toret);
    }
    
    Or even better (IMO, since I generally don't like allocating and freeing memory in different functions):
    Code:
    void func(struct mystruct *astruct) {
        astruct->myvar = 2;
    }
    
    int main(void) {
        struct mystruct *astruct = calloc(1, sizeof(astruct));
        func(astruct);
        free(astruct);
        return(0);
    }
    
    Returning a pointer to the stack would indeed be a bad thing to do.
     
  15. Peyomp

    Peyomp New Member

    Joined:
    Jan 11, 2002
    Messages:
    14,017
    Likes Received:
    0
    Man, am I glad I don't have to deal with this shit in perl.
     

Share This Page