+ 1

Can a function return more than one value???

16th Dec 2016, 6:29 PM
sujata pradhan
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; }
16th Dec 2016, 8:43 PM
Caffeinated Gamer YT
Caffeinated Gamer YT - avatar
+ 1
No
7th Jan 2017, 8:06 AM
Aishwarya
Aishwarya - avatar
0
no directly it can return only one value. if you need to return more values you should use pointers.
16th Dec 2016, 7:01 PM
hiteshposhia
hiteshposhia - avatar
0
you can do it by pointer(call by reference)
16th Dec 2016, 7:07 PM
hiteshposhia
hiteshposhia - avatar
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.
18th Dec 2016, 12:41 PM
Stephan Kadziela
Stephan Kadziela - avatar
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.
21st Dec 2016, 9:44 AM
Thomas Stone
Thomas Stone - avatar