![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Hobbyist Programmer
|
Returning two values (solved)
Is is possible to have a function to return two values instead of one?
Or do i have to have two functions for each value? printf(" The two values returned: %d and %d", funct() );Last edited by navnav; Jun 12th, 2005 at 4:47 PM. |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: here
Posts: 116
Rep Power: 0
![]() |
You will want to use a structure to do what you are asking.
example: typedef struct {
int x;
int y;
} two_int_t;From there you can either pass in a pointer to a function and fill in the values or use a structure (or pointer to) as a return value for the function. And, btw, when you are calling a printf function (or any of the varidic funtions) You want to provide enough arguments for each of the format specifiers. i.e. for two %d's you will want two arguments passed to the function. I assume you did that just for an example of your question, but thought I would comment anyway... |
|
|
|
|
|
#3 |
|
Programmer
|
Just for two integers, another option would be to provide references to the two:
bool funct(int& x, int& y) {
...
}Then you call the function like so: int x, y;
if(funct(x, y)) {
printf("The two values returned: %d and %d", x, y);
}This allows for you to return whether the function succeeded as well. Both methods put forward in this thread will work.
__________________
kirkl_uk |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: here
Posts: 116
Rep Power: 0
![]() |
Agreed, references and bool return values would suffice in C++, but for a C program, they will not be of much assistance...
|
|
|
|
|
|
#5 |
|
Troll
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4
![]() |
C and C++ both have the concept of references and pointers last I checked
|
|
|
|
|
|
#6 |
|
Programmer
|
Maybe he is referring to the bool type.
__________________
kirkl_uk |
|
|
|
|
|
#7 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,203
Rep Power: 5
![]() |
Quote:
C++ has both concepts. C only has pointers. |
|
|
|
|
|
|
#8 |
|
Expert Programmer
Join Date: Dec 2004
Posts: 794
Rep Power: 4
![]() |
C has references?
|
|
|
|
|
|
#9 | |
|
PFO God In Training
![]() Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 516
Rep Power: 4
![]() |
Quote:
// c++ reference
void foo(int& x)
{
x = 0;
}
// C reference
void foo(int* x)
{
*x = 0;
} |
|
|
|
|
|
|
#10 | |
|
Professional Programmer
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4
![]() |
Quote:
I was also under the impression that standard C does not specify the use of references.. just goes to show what I know...
__________________
-Steven "Is this a piece of your brain?" - Basil Fawlty Last edited by stevengs; Jun 12th, 2005 at 11:36 PM. |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|