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; }
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).
couldnt you just send the addresses of a variable(s) and change
it int he function and return nothing while still getting the
information.
im not actually fimilar with doing this, and i dont knowCode:void function(int &a, int &b) { //do something to a and b }
if memory leak can occur with this, but ive seen it discussed
a few times before so i might be a decent solution?
No memory leak here, this is an excellent way to handle the issue. The struct/class method just makes packaging easier, potentially making maintenance easier, that is all.
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
that's really a caller responsabilityOriginally Posted by grumpy