return more than one value in a function

Hello everyone,

I know that a c++ function could return only one value, well what if i need to return more than one value??

Eg. A function that scales the 3 dimensions of a rectangular prism: scale(scaling factor, length, width, height) and updates/returns the values.

Thanks for any help!
There are two ways you could do this:
1
2
3
4
5
6
struct MyStruct{
    int variable1, variable2;//...
    char* some_string;
};
MyStruct MyFunction(){
}

or
1
2
void MyFunction(int& variable1, int& variable2, char*& string){
}

The second one isn't really returning, but that works too.
If you make int* instead of int& you can make a function that can have some parameters set to 0. This is useful if the user only wants to get a few of them.
Or you could throw the values you want returned into some parentheses. But it would need to be one value. i.e.

 
return (length + width + height); // or whatever you're trying to accomplish 


I'd recommend passing the parameters by reference and modifying them within the function like hamsterman said.
Last edited on
yoke88 wrote:
Or you could throw the values you want returned into some parentheses. But it would need to be one value. i.e.
return (length + width + height); // or whatever you're trying to accomplish



That's silly
Topic archived. No new replies allowed.