+4

Is there any function that can return value of more than one type?

eg. int type function return int value

3/9/2017 7:42:49 AM

Shashwat Garg

21 Answers

New Answer

+11

For returning two values we can just go for a std::pair (usually typedef'd).We can also use boost::tuple (in C++11 and newer, there's std::tuple) which is basically for more than two return results.

+7

What I've done to accomplish this is to create a struct with the two data types I want and then create and return an object of that struct in the function. For example: struct TwoValues { int myInt; string myString; }; TwoValues MyFunction() { TwoValues obj; obj.myInt = 4; obj.myString = "Hello"; return obj; } int main() { TwoValues twoValues; twoValues = MyFunction(); cout << twoValues.myInt << endl; // outputs 4 cout << twoValues.myString << endl; // outputs Hello return 0; }

+7

You can use class for this purpose. Define a class with your desired components (as many as you need). Then create an object of that class inside your function and return it. So in this case your return type should be your class name which will consist of multiple components with different data types.

+5

An easy way to do this is to use reference. Add another parameter which is a reference of a variable, assign the value you want to return to it, then you can use the value out of that function.

+2

template<class T> T returnval (T val){ //some code. return val; }

+1

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main(string[] args) { int a; string b; GetValues(out a, out b, 3); Console.WriteLine(a*5+" "+b); } static void GetValues(out int x, out string y, int z) { x = z*2; y = "stringOutput:"+(z*3).ToString(); } } }

+1

We can return void* pointer to any datatype, overload function, use reference or union/struct

+1

Just put needed fields as an argument and You'll get them operated. Making a class is pointless,

+1

the template function can return any data type

+1

yes by using general purpose pointer.the syntax will be with void and use static_cast<>

+1

you can return a generic type instead

0

one way you can return more than 2 in Java is making the variable into an array and return array

0

for Java you can only return one value. you can pass as many arguments. and can initialize as many instances variables using constructors. hope it helps

0

give a simple code method

0

Tuples are used to do this.

0

if you want to return more than 1 value, then use coid function with pass by reference parameters

0

can these return values be of different types

-1

NO, you cannot return multiple data types from a single function. i.e if a function returns int. it Must return an integer.. However you can manipulated the value of that integer but not the data type itself. as stated in the answers, they have used multiple functions.NO SINGLE FUNCTION CAN RETURN MULTIPLE DATA TYPES

-1

yes, actually you can return more than 2 variables by making a class with variables you want to return as data members and then creating an object of that class and returning that object from the function you want

-2

I want to learn web designing, what are the basic things I need to learn