is it possible to return more than one value?
im writing a prog where i send a value to a function but i want that function to return 4 integers. Will it be easier/wiser to use global variables??
is it possible to return more than one value?
im writing a prog where i send a value to a function but i want that function to return 4 integers. Will it be easier/wiser to use global variables??
`Who are YOU?' said the Caterpillar.
This was not an encouraging opening for a conversation. Alice replied, rather shyly, `I--I hardly know, sir, just at present-- at least I know who I WAS when I got up this morning, but I think I must have been changed several times since then.' - Lewis Caroll's Alice in Wonderland.
NO! But you can use array, and then return the pointer to the first element of the array.
so i store those 4 integers in an array,return the pointer to the array and use a loop to print them,right?
`Who are YOU?' said the Caterpillar.
This was not an encouraging opening for a conversation. Alice replied, rather shyly, `I--I hardly know, sir, just at present-- at least I know who I WAS when I got up this morning, but I think I must have been changed several times since then.' - Lewis Caroll's Alice in Wonderland.
The array should be defined out of the function which is called. You can define the array inside function main or somewhere else.
The array should be defined out of the function which is called. You can define the array inside function main, or somewhere else.
I wouldn't do this with an array. Having the caller pass an array is clumsy (eg an opportunity for the caller to pass an array that is too short). Using a global arrays imposes some limitations on callers (eg the function can't be called multiple times between two sequence points, and often cannot be recursive). Having the function dynamically allocate the array to return as a pointer is an opportunity for a memory leak.
My prefered way would be to return a structure (or a class), eg
I'm assuming you don't want to simply pass four arguments by pointer or reference.Code:struct X { int a,b c,d; }; X function() { X x; // set members of x return x; }
that's really a caller responsabilityOriginally Posted by grumpy
So, i declare an array and then just return a pointer to the array and use a counter to display/use the data in the array.
how will te code look like ? something like return *pi;??
`Who are YOU?' said the Caterpillar.
This was not an encouraging opening for a conversation. Alice replied, rather shyly, `I--I hardly know, sir, just at present-- at least I know who I WAS when I got up this morning, but I think I must have been changed several times since then.' - Lewis Caroll's Alice in Wonderland.
If you use grumpy's code.. itd be like this (and of course you could pass parameters to aFunction instead):
Looking at that makes me cry.. it looks like such an inefficient method, but then again its late and I've never tried anything like that before.. I'll see in the morning if I can come up with any better ideas for passing lists, for own benefit.Code:#include <iostream> using namespace std; struct theStruct { int a,b,c,d; }; theStruct aFunction(); int main() { theStruct anObject; anObject = aFunction(); cout << anObject.a << endl; cout << anObject.b << endl; cout << anObject.c << endl; cin.get(); } theStruct aFunction() { theStruct theObject; theObject.a = 1; theObject.b = 2; theObject.c = 3; theObject.d = 4; return theObject; }
Looking at it it looks like I should return a pointer and make it = new theObject, but I cant think of how I'd recieve it.. its too late, hope it gives you something to work with.
Last edited by Dae; 06-22-2005 at 02:59 AM.
Warning: Have doubt in anything I post.
GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101
Well, you could return a vector of integers.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
how about a struct of stuff
- "Problem Solving C++, The Object of Programming" -Walter Savitch
- "Data Structures and Other Objects using C++" -Walter Savitch
- "Assembly Language for Intel-Based Computers" -Kip Irvine
- "Programming Windows, 5th edition" -Charles Petzold
- "Visual C++ MFC Programming by Example" -John E. Swanke
- "Network Programming Windows" -Jones/Ohlund
- "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
- "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel
In this particular case you wind up copying the structure (some optimizing compilers are smart enough to avoid this, though). If, on the other hand, you pass in a pointer to the struct (or a reference to the class) then you avoid the potential for the copy.
In case someone things to return a pointer to the local value of x, that is dangerous and will inevitably lead to errors and debugging nightmares as x is allocated on the stack and will be overwritten. The use of a static instance will work as long as you do not require reentrancy (for recursive functions and/or multi-threading), but it is best to simply pass in a pointer/reference.
Free code: http://sol-biotech.com/code/.
It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
--Me, I just made it up
The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
--George Bernard Shaw
You guys lost me. I think im to much of a novice to understand. Will anybody show me the way to become a good programmer??
`Who are YOU?' said the Caterpillar.
This was not an encouraging opening for a conversation. Alice replied, rather shyly, `I--I hardly know, sir, just at present-- at least I know who I WAS when I got up this morning, but I think I must have been changed several times since then.' - Lewis Caroll's Alice in Wonderland.
Just keep on keepin on. I have probably read close to 10,000 pages of computer science related material and have been a professional programmer for at least a decade (actual calculation will make me feel too old to justify the effort). I didn't have forums like these when I was getting started, they go a long way to helping get through the learning curve.
WRT the copying, when a function returns a value that value is copied to a location on the stack. If the calling function actually uses the return value, it is typically copied from the stack to the memory location given in the calling function. For a couple of ints, that is not a big deal, but C++ objects can get huge fast and if you are doing this in a tight loop you can wind up spending 99% of your time copying return values around.
Free code: http://sol-biotech.com/code/.
It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
--Me, I just made it up
The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
--George Bernard Shaw
I wouldn't worry about the efficiency of copying at this point. Just use grumpy's method (except for the missing comma).