Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 12th, 2005, 11:51 AM   #1
Navid
Hobbyist Programmer
 
Navid's Avatar
 
Join Date: Feb 2005
Location: Canada
Posts: 187
Rep Power: 4 Navid is on a distinguished road
Send a message via MSN to Navid
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.
Navid is offline   Reply With Quote
Old Jun 12th, 2005, 12:35 PM   #2
L7Sqr
Hobbyist Programmer
 
Join Date: Jun 2005
Location: here
Posts: 114
Rep Power: 0 L7Sqr is an unknown quantity at this point
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...
L7Sqr is online now   Reply With Quote
Old Jun 12th, 2005, 12:45 PM   #3
kirkl_uk
Programmer
 
kirkl_uk's Avatar
 
Join Date: Apr 2005
Location: England
Posts: 86
Rep Power: 4 kirkl_uk is on a distinguished road
Send a message via MSN to kirkl_uk
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
kirkl_uk is offline   Reply With Quote
Old Jun 12th, 2005, 12:55 PM   #4
L7Sqr
Hobbyist Programmer
 
Join Date: Jun 2005
Location: here
Posts: 114
Rep Power: 0 L7Sqr is an unknown quantity at this point
Agreed, references and bool return values would suffice in C++, but for a C program, they will not be of much assistance...
L7Sqr is online now   Reply With Quote
Old Jun 12th, 2005, 1:58 PM   #5
Dameon
Troll
 
Dameon's Avatar
 
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4 Dameon is on a distinguished road
C and C++ both have the concept of references and pointers last I checked
Dameon is offline   Reply With Quote
Old Jun 12th, 2005, 2:04 PM   #6
kirkl_uk
Programmer
 
kirkl_uk's Avatar
 
Join Date: Apr 2005
Location: England
Posts: 86
Rep Power: 4 kirkl_uk is on a distinguished road
Send a message via MSN to kirkl_uk
Maybe he is referring to the bool type.
__________________
kirkl_uk
kirkl_uk is offline   Reply With Quote
Old Jun 12th, 2005, 2:19 PM   #7
uman
Expert Programmer
 
Join Date: Dec 2004
Posts: 794
Rep Power: 4 uman is on a distinguished road
C has references?
uman is offline   Reply With Quote
Old Jun 12th, 2005, 2:21 PM   #8
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 498
Rep Power: 4 Ancient Dragon is on a distinguished road
Quote:
Originally Posted by uman
C has references?
yes
// c++ reference
void foo(int& x)
{
   x = 0;
}

// C reference
void foo(int* x)
{
   *x = 0;
}
Ancient Dragon is offline   Reply With Quote
Old Jun 12th, 2005, 2:28 PM   #9
stevengs
Professional Programmer
 
stevengs's Avatar
 
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4 stevengs is on a distinguished road
Quote:
Originally Posted by Ancient Dragon
well, I reckon I am more confused than I had originally believed myself to be. I was certain that there were differences between references and pointers. My main argument would probably be: "Then why be there??" Isn't the use of references more restrictive (i.e. for safety) ?

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.
stevengs is offline   Reply With Quote
Old Jun 12th, 2005, 2:35 PM   #10
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
There is definitely a difference between a "reference" per C++ definition and a pointer. The choice of the term, "reference" for C++, was a little shaky, as pointers, labels, nicknames, aliases, are ALL references. However, one cannot pass a "reference" (as defined in C++) in C. The "&" operator is strictly an address-of operator and the use inside the function has to be the same as when dereferencing a pointer. In C++, on the other hand, a "reference" is an alias and a passed reference amounts to one fewer levels of indirection.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:17 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC