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
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;
}
+ 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.
+ 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.
+ 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.
+ 4
template<class T>
T returnval (T val){
//some code.
return val;
}
+ 2
We can return void* pointer to any datatype, overload function, use reference or union/struct
+ 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";
}
}
}
+ 1
if you want to return more than 1 value, then use coid function with pass by reference parameters
+ 1
Tuples are used to do this.
+ 1
you can return a generic type instead
+ 1
yes by using general purpose pointer.the syntax will be with void and use static_cast<>
+ 1
the template function can return any data type
+ 1
Just put needed fields as an argument and You'll get them operated.
Making a class is pointless,
+ 1
one way you can return more than 2 in Java is making the variable into an array and return array
0
can these return values be of different types
0
give a simple code method
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
- 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
- 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
- 3
I want to learn web designing, what are the basic things I need to learn