Questions tagged [return-type]

This tag is for questions dealing with the type of the variable or value returned by a function.

return-type
Filter by
Sorted by
Tagged with
844 votes
19 answers
460k views

What should main() return in C and C++?

What is the correct (most efficient) way to define the main() function in C and C++ — int main() or void main() — and why? And how about the arguments? If int main() then return 1 or return 0?
Joel's user avatar
  • 15.4k
683 votes
4 answers
422k views

How to specify multiple return types using type-hints

I have a function in python that can either return a bool or a list. Is there a way to specify the return types using type hints? For example, is this the correct way to do it? def foo(id) -> list ...
Yahya Uddin's user avatar
  • 27.9k
375 votes
17 answers
354k views

Apply pandas function to column to create multiple new columns?

How to do this in pandas: I have a function extract_text_features on a single text column, returning multiple output columns. Specifically, the function returns 6 values. The function works, however ...
smci's user avatar
  • 33.3k
220 votes
7 answers
415k views

How do I make the return type of a method generic?

Is there a way to make this method generic so I can return a string, bool, int, or double? Right now, it's returning a string, but if it's able find "true" or "false" as the ...
JustBeingHelpful's user avatar
204 votes
3 answers
121k views

Nullable return types in PHP7

PHP 7 introduces return type declarations. Which means I can now indicate the return value is a certain class, interface, array, callable or one of the newly hintable scalar types, as is possible for ...
Jeroen De Dauw's user avatar
189 votes
1 answer
202k views

C++: variable 'std::ifstream ifs' has initializer but incomplete type

Sorry if this is pretty noobish, but I'm pretty new to C++. I'm trying to open a file and read it using ifstream: vector<string> load_f(string file) { vector<string> text; ifstream ...
beakr's user avatar
  • 5,797
146 votes
4 answers
46k views

What's the difference between returning void and returning a Task?

In looking at various C# Async CTP samples I see some async functions that return void, and others that return the non-generic Task. I can see why returning a Task<MyType> is useful to return ...
James Cadd's user avatar
  • 12.2k
146 votes
4 answers
20k views

Why doesn't C# support the return of references?

I have read that .NET supports return of references, but C# doesn't. Is there a special reason? Why I can't do something like: static ref int Max(ref int x, ref int y) { if (x > y) return ...
Tomas Ramirez Sarduy's user avatar
129 votes
16 answers
156k views

Returning anonymous type in C#

I have a query that returns an anonymous type and the query is in a method. How do you write this: public "TheAnonymousType" TheMethod(SomeParameter) { using (MyDC TheDC = new MyDC()) { var ...
frenchie's user avatar
  • 52.6k
129 votes
5 answers
224k views

Return from lambda forEach() in java

I am trying to change some for-each loops to lambda forEach()-methods to discover the possibilities of lambda expressions. The following seems to be possible: ArrayList<Player> playersOfTeam = ...
samu's user avatar
  • 1,996
124 votes
4 answers
36k views

Why can't 'kotlin.Result' be used as a return type?

I've created a method, and the return is Result<R> in a class of MyClass<R>, but the error message is: 'kotlin.Result' cannot be used as a return type I've also looked into the Result ...
ersin-ertan's user avatar
  • 2,293
100 votes
6 answers
78k views

Java generics void/Void types

I am implementing a ResponseHandler for the apache HttpClient package, like so: new ResponseHandler<int>() { public int handleResponse(...) { // ... code ... return 0; } ...
Travis Webb's user avatar
  • 14.8k
100 votes
4 answers
65k views

PHP 7 interfaces, return type hinting and self

UPDATE: PHP 7.4 now does support covariance and contravariance which addresses the major issue raised in this question. I have run into something of an issue with using return type hinting in PHP 7....
GordonM's user avatar
  • 31.5k
98 votes
8 answers
129k views

C# numeric enum value as string

I have the following enum: public enum Urgency { VeryHigh = 1, High = 2, Routine = 4 } I can fetch an enum "value" as string like this: ((int)Urgency.Routine).ToString() // returns ...
David Moorhouse's user avatar
98 votes
9 answers
159k views

Can two Java methods have same name with different return types? [duplicate]

Can two Java methods have the same name with different return type? The return type of the methods are different and they are declared with the same method's name. Is that allowed?
jenifer's user avatar
  • 981
93 votes
3 answers
67k views

C++ virtual function return type

Is it possible for an inherited class to implement a virtual function with a different return type (not using a template as return)?
zzzbbx's user avatar
  • 10.9k
88 votes
12 answers
81k views

Is it safe to return a struct in C or C++?

What I understand is that this shouldn't be done, but I believe I've seen examples that do something like this (note code is not necessarily syntactically correct but the idea is there) typedef ...
jzepeda's user avatar
  • 1,490
74 votes
4 answers
4k views

Why does Enumeration get converted to ArrayList and not List in java.utils?

Is there a good reason that the Collections.list() method in the java.utils package returns an ArrayList<T> instead of List<T>? Obviously an ArrayList is a List, but I'm under the ...
Tianxiang Xiong's user avatar
70 votes
4 answers
77k views

Return CompletableFuture<Void> or CompletableFuture<?>?

I want to write an asynchronous method that returns a CompletableFuture. The only purpose of the future is to track when the method is complete, not its result. Would it be better to return ...
John Kugelman's user avatar
65 votes
5 answers
85k views

Void as return type

I was testing return types with PHP 7. I've created a simple script to test return types of PHP 7: <?php Class Obj { public function __construct(){ } public function test(): ...
Daan's user avatar
  • 12.2k
65 votes
3 answers
91k views

What if I write return statement in constructor?

What if I write return statement in constructor? Is it standard conformant? struct A { A() { return; } }; The above code compiles fine, without any error at ideone. But the following code ...
Sarfaraz Nawaz's user avatar
57 votes
4 answers
12k views

Simplest way to determine return type of function

Given a very simple, but lengthy function, such as: int foo(int a, int b, int c, int d) { return 1; } // using ReturnTypeOfFoo = ??? What is the most simple and concise way to determine the ...
Cybran's user avatar
  • 2,263
57 votes
14 answers
9k views

Why can't I explicitly return void from a method?

void run() { ... if (done) return cancel(); ... } where cancel() return void. This won't compile... and I can almost understand why. But if I want to return a void from a void, why not? ...
Travis Webb's user avatar
  • 14.8k
57 votes
3 answers
41k views

Should I use Unit or leave out the return type for my scala method?

I am not sure what the difference is between specifying Unit as the return type of my scala method or leaving out the return type altogether. What is the difference? Can anyone please advise?
balteo's user avatar
  • 24.1k
56 votes
7 answers
82k views

Return multiple values from a Java method: why no n-tuple objects?

Why isn't there a (standard, Java certified) solution, as part of the Java language itself, to return multiple values from a Java method, rather than developers having to use their own means, such as ...
Saket's user avatar
  • 45.9k
50 votes
1 answer
712 views

Unexpected implicit resolution based on inference from return type

Given a typeclass where instance selection should be performed based on the return type: case class Monoid[A](m0: A) // We only care about the zero here implicit def s[T] : Monoid[Set[T]] = Monoid(...
Patrick Prémont's user avatar
46 votes
1 answer
70k views

How to specify method return type list of (what) in Python?

Let's say I have a method like the following def validate(self, item:dict, attrs:dict)-> list: If I want to be more specific and tell that my return type is a list of ValidationMessages? How ...
koalaok's user avatar
  • 5,364
45 votes
3 answers
61k views

Why do constructors in java not have a return type? [duplicate]

Possible Duplicate: Why constructor not returns value Why don't constructors have a return type, not even void? What's the reason for that?
user845932's user avatar
44 votes
5 answers
63k views

Can a C# method return a method?

Can a method in C# return a method? A method could return a lambda expression for example, but I don't know what kind of type parameter could I give to such a method, because a method isn't Type. ...
Miro's user avatar
  • 1,798
44 votes
14 answers
190k views

Return different type of data from a method in java?

public static void main(String args[]) { myMethod(); // i am calling static method from main() } . public static ? myMethod(){ // ? = what should be the return type return value;// is ...
Ruchira Gayan Ranaweera's user avatar
42 votes
10 answers
6k views

Is there a standard "never returns" attribute for C# functions?

I have one method that looks like this: void throwException(string msg) { throw new MyException(msg); } Now if I write int foo(int x, y) { if (y == 0) throwException("Doh!"); ...
user192472's user avatar
40 votes
7 answers
33k views

Why should functions always return the same type?

I read somewhere that functions should always return only one type so the following code is considered as bad code: def x(foo): if 'bar' in foo: return (foo, 'bar') return None I guess the ...
self.self's user avatar
  • 417
40 votes
7 answers
16k views

How can I return an anonymous struct in C?

Trying some code, I realized that the following code compiles: struct { int x, y; } foo(void) { } It seems as if we are defining a function named foo which returns an anonymous struct. Does it only ...
Askaga's user avatar
  • 6,181
40 votes
2 answers
23k views

SQL function return-type: TABLE vs SETOF records

What's the difference between a function that returns TABLE vs SETOF records, all else equal. CREATE FUNCTION events_by_type_1(text) RETURNS TABLE(id bigint, name text) AS $$ SELECT id, name FROM ...
ma11hew28's user avatar
  • 124k
37 votes
4 answers
9k views

Return Type Covariance with Smart Pointers

In C++ we can do this: struct Base { virtual Base* Clone() const { ... } virtual ~Base(){} }; struct Derived : Base { virtual Derived* Clone() const {...} //overrides Base::Clone }; ...
Armen Tsirunyan's user avatar
36 votes
6 answers
19k views

When is C++ covariance the best solution?

This question was asked here a few hours ago and made me realise that I have never actually used covariant return types in my own code. For those not sure what covariance is, it's allowing the return ...
user avatar
34 votes
12 answers
50k views

PHP class method return type

Is it possible to define the return type like this? public static function bool Test($value) { return $value; //this value will be bool }
user avatar
32 votes
3 answers
2k views

Can we have a function with multiple return types? (in C++11 and above)

I am trying to write a generic function which takes input as uint8, uint16, uint32, uint64, .... and return the maximum value with the datatype of largest element? For example: template < ...
Kiran ND's user avatar
  • 327
31 votes
6 answers
16k views

How to get the return type of a method call in IntelliJ?

Normally you would expect just hovering over a method it would show a popup of the return type. How do you get this information in intellij ultimate?
loyalflow's user avatar
  • 14.6k
30 votes
2 answers
46k views

What does Void return type mean in Kotlin

I tried to create function without returning value in Kotlin. And I wrote a function like in Java but with Kotlin syntax fun hello(name: String): Void { println("Hello $name"); } And I've got an ...
Mara's user avatar
  • 3,007
30 votes
1 answer
19k views

Return None from python function annotated with mypy, multiple return types

I come from a Typescript background. I'm bringing static type checking into a python project I'm working on (using mypy). In Typescript, it is valid to return null from a function that is annotated ...
Corey Cole's user avatar
  • 2,382
30 votes
7 answers
14k views

Why does Array.prototype.push return the new length instead of something more useful?

Ever since its introduction in ECMA-262, 3rd Edition, the Array.prototype.push method's return value is a Number: 15.4.4.7 Array.prototype.push ( [ item1 [ , item2 [ , … ] ] ] ) The arguments ...
Bryce's user avatar
  • 6,560
28 votes
4 answers
15k views

Why should the assignment operator return a reference to the object?

I'm doing some revision of my C++, and I'm dealing with operator overloading at the minute, specifically the "="(assignment) operator. I was looking online and came across multiple topics discussing ...
maccard's user avatar
  • 1,218
27 votes
2 answers
22k views

ActiveRecord 'destroy' method returns a boolean value in Ruby on Rails?

I am using Ruby on Rails 3 and I would like to know what type of return will have the following code: @user.destroy I need that to handle cases on success and fault in someway like this: if @user....
user502052's user avatar
  • 15.1k
24 votes
7 answers
65k views

Dynamic Return Type in Java method

I've seen a question similar to this multiple times here, but there is one big difference. In the other questions, the return type is to be determined by the parameter. What I want/need to do is ...
Jon Egeland's user avatar
  • 12.5k
24 votes
12 answers
60k views

How to return local array in C++?

char *recvmsg(){ char buffer[1024]; return buffer; } int main(){ char *reply = recvmsg(); ..... } I get a warning: warning C4172: returning address of local variable or temporary
user avatar
24 votes
4 answers
26k views

php7 void return type not working?

I have a problem with return types in php7, specially "void". it works with all other types, int, string, null, bool, class objects. but when i use void it expecting me to return an instance of ...
nSams Dev's user avatar
  • 707
23 votes
4 answers
28k views

Returning an objects subclass with generics

With an abstract class I want to define a method that returns "this" for the subclasses: public abstract class Foo { ... public <T extends Foo> T eat(String eatCake) { ....
Sarabjot's user avatar
  • 519
22 votes
12 answers
4k views

Why does Java Specifications want the main method of a program to be void only? [duplicate]

Although the return type is not a part of a method's signature, JVM looks for exact declaration as public static void main(String[] args) My assumption is that since method signature does not have "...
Mohit Kanwar's user avatar
  • 3,010
20 votes
8 answers
56k views

return empty List in catch block

I have a c# function that reads file locations from a Datatable, and returns a List with all the file lcoations to the calling method. In the Catch block, I want to return an empty list with a false ...
Our Man in Bananas's user avatar

1
2 3 4 5
31