C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 02-17-2002, 02:29 PM   #1
tim
Guest
 
Posts: n/a
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?
  Reply With Quote
Old 02-17-2002, 02:35 PM   #2
Unregistered
Guest
 
Posts: n/a
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;
}
  Reply With Quote
Old 02-17-2002, 03:09 PM   #3
Code Goddess
 
Prelude's Avatar
 
Join Date: Sep 2001
Posts: 9,664
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.
Prelude is offline   Reply With Quote
Old 02-18-2002, 01:02 AM   #4
Unregistered
Guest
 
Posts: n/a
In C a function can only return one variable, so you'll have to use duplicate functions or pointers.
  Reply With Quote
Old 02-18-2002, 12:17 PM   #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.
Shiro is offline   Reply With Quote
Old 02-18-2002, 02:39 PM   #6
Unregistered
Guest
 
Posts: n/a
Shiro, I like your interesting solutions!
  Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 07:55 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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