Is there any function that can return value of more than one type? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

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

eg. int type function return int value

9th Mar 2017, 7:42 AM
Shashwat Garg
Shashwat Garg - avatar
21 Answers
+ 9
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; }
14th Mar 2017, 6:02 PM
Zeke Williams
Zeke Williams - avatar
+ 19
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.
9th Mar 2017, 11:36 AM
Shamima Yasmin
Shamima Yasmin - avatar
+ 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.
9th Mar 2017, 7:50 AM
Sunny Saurabh
Sunny Saurabh - avatar
+ 6
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.
9th Mar 2017, 4:05 PM
王鹏
王鹏 - avatar
+ 4
template<class T> T returnval (T val){ //some code. return val; }
19th Mar 2017, 5:55 AM
Prabhakar Dev
Prabhakar Dev - avatar
+ 2
We can return void* pointer to any datatype, overload function, use reference or union/struct
16th Mar 2017, 6:29 AM
SUPER_S
SUPER_S - avatar
+ 2
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, "Hello" ); int myInt=a; string myString = b; Console.WriteLine(myInt); //output 9 Console.WriteLine(myString); //output Hello world } static void GetValues(out int x, out string y, int z, string q) { x = z*z; y = q+" world"; } } }
17th Mar 2017, 11:35 AM
Barış
Barış - avatar
+ 1
if you want to return more than 1 value, then use coid function with pass by reference parameters
9th Mar 2017, 5:13 PM
QuRatulain Soomro
QuRatulain Soomro - avatar
+ 1
Tuples are used to do this.
9th Mar 2017, 10:00 PM
Matt George
Matt George - avatar
+ 1
you can return a generic type instead
10th Mar 2017, 5:36 AM
POJO
POJO - avatar
+ 1
yes by using general purpose pointer.the syntax will be with void and use static_cast<>
10th Mar 2017, 3:02 PM
Muhammad Abubakkar
Muhammad Abubakkar - avatar
+ 1
the template function can return any data type
13th Mar 2017, 5:47 PM
Gloria E. Hanna
Gloria E. Hanna - avatar
+ 1
Just put needed fields as an argument and You'll get them operated. Making a class is pointless,
15th Mar 2017, 2:55 PM
Michał Bujakowski
Michał Bujakowski - avatar
+ 1
one way you can return more than 2 in Java is making the variable into an array and return array
17th Mar 2017, 5:25 AM
Fmbinsum Fmbinsum
0
can these return values be of different types
9th Mar 2017, 10:49 AM
Shashwat Garg
Shashwat Garg - avatar
0
give a simple code method
15th Mar 2017, 6:26 AM
vivek kumar
vivek kumar - avatar
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
17th Mar 2017, 5:24 AM
Fmbinsum Fmbinsum
- 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
14th Mar 2017, 11:50 AM
Nihit
Nihit - avatar
- 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
18th Mar 2017, 5:55 AM
Anubhav Malik
Anubhav Malik - avatar
- 3
I want to learn web designing, what are the basic things I need to learn
15th Mar 2017, 3:10 PM
Prosper Owumi
Prosper Owumi - avatar