functions to return 2 variables?

This is a discussion on functions to return 2 variables? within the C Programming forums, part of the General Programming Boards category; i'm trying to have a function return 2 values to 2 separate variables. i know the idea is for a ...

  1. #1
    tim
    Guest

    Angry functions to return 2 variables?

    i'm trying to have a function return 2 values to 2 separate variables. i know the idea is for a function to only do 1 specific thing, and thats pretty much what my functions are doing. the only difference is, depending on user input, 1 of 2 different variables will be modified. SO, at the end of this function, i have it return 2 variables - 1 has been modified, 1 is the same as when the function began.

    i COULD go back and make 2 functions, which may be what i have to do, but it'd be really convenient if there was a way to be able to return multiple variables.

    this is what i have tried thus far - this is similar to the format in other programming languages (mainly math-oriented ones) so i thought it might work, but no luck:

    [int, int] functionname(variable1, variable2, variable3)

    then, in the main program, to call it, i put:

    [mainvariable1, mainvariable2]=functionname(variable1, variable2, variable3);

    and within the function i put:

    return(mainvariable1, mainvariable2);

    hoping that would do the trick to return 2 variables. it didn't.

    any help?

  2. #2
    Unregistered
    Guest
    Well, either you can return one value and write the other one through a pointer to a variable defined in main, write both through pointers, or return a pointer to an array of two variables defined in the function, or simply return a pointer.

    1) int function( int firstval, int *secondval )
    {
    *secondval = ...;
    return firstval;
    }

    ...

    variable = function( variable, &variable2 );
    printf( "%d %d", variable, variable2 );

    2) typedef int ( *ptr_to_ar )[2];
    ptr_to_ar function( int firstval, int secondval )
    {
    static int intar[2];
    ptr_to_ar ptr = &intar;
    return ptr_to_ar;
    }

    3) int *function( )
    {
    int *ptr = malloc( x * sizeof( int ) );
    return ptr;
    }

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    You can approach this several different ways, the pointer way:
    Code:
    void function ( int *varOne, int *varTwo );
    /* The call */
    function ( foo, bar );
    The non-pointer return one way:
    Code:
    int function ( int varOne, int VarTwo ) {
      if ( something for varOne ) {
        /* Set the value */
        return varOne;
      }
      else if ( something for varTwo ) {
        /* Set the value */
        return varTwo;
      else
        return /* error value goes here */
    }
    Or you can just wrap both variables in a stuct and return that.

    There are many ways, but generally if you need to return two values your design is flawed and should be looked at.

    -Prelude
    My best code is written with the delete key.

  4. #4
    Unregistered
    Guest
    In C a function can only return one variable, so you'll have to use duplicate functions or pointers.

  5. #5
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    Another thing you could do is defining a structure which contains both values. Fill the structure in the function and return it. Or pass a structure to the function and let it be filled.

    You could also use an array, which can be filled or returned by the function.

  6. #6
    Unregistered
    Guest
    Shiro, I like your interesting solutions!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  3. Linking OpenGL in Dev-C++
    By linkofazeroth in forum Game Programming
    Replies: 4
    Last Post: 09-13-2005, 10:17 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. Algorithm to walk through a maze.
    By Nutshell in forum C Programming
    Replies: 30
    Last Post: 01-21-2002, 12:54 AM

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