¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita+ 2
Does return type only used for returning integer?
6 Respuestas
+ 5
you can return Strings, intergers,double ect
+ 4
Absolutely no brother,
first of all, "RETURN IS NOT A DATA TYPE, IT'S A STATEMENT". which is used to shift the control to the line in program, from which place , you called the function.
like:
char function_name(char ch)
{
some code
return ch;
}
int main()
{
cout << function_name('A');
return 0;
}
here, when we calling the function by passing 'A' as argument, control transferred to function. After some job, it comes to return statement, and returns ch. Then again comes to main function.
That's the use of return statement.
return can be used to return various things in any programming language.
like it can be used to return data of primitive datatype.
int, float, char, double, long, string, Boolean and so on.
It can user-defined datatypes
struct, union, pointer, ADT(stack, queue, and other DATA STRUCTURE constructs).
it can be objects also, like some people's are already told.
like :
struct Stack{
int data;
struct Stack *next;
}S;
struct Stack* INSERT(int x)
{
struct Stack* newNode = new struct Stack;
newNode->data = x;
newNode->next = S;
return newNode;
}
int main()
{
S = INSERT(5);
return 0;
}
here, you can see we are returning an ADT(Abstract Data Type) which is a user defined data.
Now I guess, you understood, we can return other things also except integer.
You have any doubt after that, you can ask.
+ 3
No, you can return double, void, int....
+ 2
You can return an data type, even objects you have created.
The only aception is the main function which must return void or int.
+ 1
it return double int float string etc
+ 1
it can return any type of value depending on the type of the function