![]() |
| | #1 |
| Guest
Posts: n/a
| 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 |
| 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; } |
|
| | #3 |
| Code Goddess 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 ); 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 */
}
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 | |
| | #4 |
| Guest
Posts: n/a
| In C a function can only return one variable, so you'll have to use duplicate functions or pointers. |
|
| | #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 | |
| | #6 |
| Guest
Posts: n/a
| Shiro, I like your interesting solutions! |
|
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |