+ 1
Can a function return more than one value???
6 Respostas
+ 5
No.
In C++ a function can return just one value. However, if you want to receive data of multiple values you can define a class with multiple variables. In the called function you can create a new object of this class and set values for it's mutiple data-variables and then return the object of that class.
So, ultimately you are still returning just one value(object) but that object contains multiple values.
e.g.// emitting access specifiers and get() set() for simplicity.
class A{
int x,y;
}
A called_func(int a,int b){
A obj=new A();
obj.x=a+b;
obj.y=a-b;
return obj;
}
+ 1
No
0
no directly it can return only one value. if you need to return more values you should use pointers.
0
you can do it by pointer(call by reference)
0
normaly a function can only return one value. the easiest way to return more than one value of the same type is using containers. If you need to return different types than you need your own function.
0
Technically, no. Conceptually you can, though. The STL actually provides some rather nice containers that can be used to this extent, such as std::pair<>, std::tuple<>, and std::vector<>. You'd be returning an object that contains the other variables you want to provide to the caller. You'd have to learn how to work with these containers, however.