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??

06-21-2005

Antigloss

NO! But you can use array, and then return the pointer to the first element of the array.

06-21-2005

Marlon

so i store those 4 integers in an array,return the pointer to the array and use a loop to print them,right?

06-21-2005

Antigloss

The array should be defined out of the function which is called. You can define the array inside function main or somewhere else.

06-21-2005

Antigloss

The array should be defined out of the function which is called. You can define the array inside function main, or somewhere else.

06-21-2005

grumpy

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

Code:

struct X
{
int a,b c,d;
};

X function()
{
X x;
// set members of x
return x;
}

I'm assuming you don't want to simply pass four arguments by pointer or reference.

06-21-2005

laserlight

Well, you could return a vector of integers.

06-21-2005

The Brain

how about a struct of stuff

06-21-2005

mitakeet

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.

06-21-2005

Marlon

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??

06-21-2005

mitakeet

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.

06-21-2005

Daved

I wouldn't worry about the efficiency of copying at this point. Just use grumpy's method (except for the missing comma).

06-21-2005

ILoveVectors

couldnt you just send the addresses of a variable(s) and change
it int he function and return nothing while still getting the
information.

Code:

void function(int &a, int &b)
{
//do something to a and b
}

im not actually fimilar with doing this, and i dont know
if memory leak can occur with this, but ive seen it discussed
a few times before so i might be a decent solution?

06-21-2005

mitakeet

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.