Questions tagged [return-value]

Return value is the result of evaluation of a return statement.

4223 questions
1147
votes
14 answers

Best way to return multiple values from a function?

The canonical way to return multiple values in languages that support it is often tupling. Option: Using a tuple Consider this trivial example: def f(x): y0 = x + 1 y1 = x * 3 y2 = y0 ** y3 return (y0, y1, y2) However, this quickly gets…
saffsd
  • 21,564
  • 16
  • 57
  • 65
757
votes
18 answers

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? There are numerous duplicates of this question,…
Joel
  • 14,042
  • 15
  • 35
  • 31
627
votes
19 answers

How do I make the method return type generic?

Consider this example (typical in OOP books): I have an Animal class, where each Animal can have many friends. And subclasses like Dog, Duck, Mouse etc which add specific behavior like bark(), quack() etc. Here's the Animal class: public class…
Sathish
  • 19,050
  • 22
  • 57
  • 69
505
votes
11 answers

How do I execute a command and get the output of the command within C++ using POSIX?

I am looking for a way to get the output of a command when it is run from within a C++ program. I have looked at using the system() function, but that will just execute a command. Here's an example of what I'm looking for: std::string result =…
Misha M
  • 9,887
  • 16
  • 46
  • 64
495
votes
20 answers

How to return a string value from a Bash function

I'd like to return a string from a Bash function. I'll write the example in java to show what I'd like to do: public String getSomeString() { return "tadaa"; } String variable = getSomeString(); The example below works in bash, but is there a…
Tomas F
  • 6,160
  • 5
  • 22
  • 31
477
votes
11 answers

Difference between return and exit in Bash functions

What is the difference between the return and exit statement in Bash functions with respect to exit codes?
lecodesportif
  • 9,527
  • 8
  • 31
  • 53
390
votes
11 answers

Return value in a Bash function

I am working with a bash script and I want to execute a function to print a return value: function fun1(){ return 34 } function fun2(){ local res=$(fun1) echo $res } When I execute fun2, it does not print "34". Why is this the case?
mindia
  • 5,039
  • 3
  • 19
  • 20
375
votes
9 answers

How can I index a MATLAB array returned by a function without first assigning it to a local variable?

For example, if I want to read the middle value from magic(5), I can do so like this: M = magic(5); value = M(3,3); to get value == 13. I'd like to be able to do something like one of these: value = magic(5)(3,3); value = (magic(5))(3,3); to…
Joe Kearney
  • 6,947
  • 5
  • 32
  • 42
301
votes
4 answers

How to return a result from a VBA function

How do I return a result from a function? For example: Public Function test() As Integer return 1 End Function This gives a compile error. How do I make this function return an integer?
Mike
  • 54,052
  • 71
  • 166
  • 213
240
votes
13 answers

How to assign from a function which returns more than one value?

Still trying to get into the R logic... what is the "best" way to unpack (on LHS) the results from a function returning multiple values? I can't do this apparently: R> functionReturningTwoValues <- function() { return(c(1, 2)) } R>…
mariotomo
  • 8,472
  • 7
  • 43
  • 63
209
votes
31 answers

Should functions return null or an empty object?

What is the best practice when returning data from functions. Is it better to return a Null or an empty object? And why should one do one over the other? Consider this: public UserEntity GetUserById(Guid userId) { //Imagine some code here to…
7wp
  • 12,082
  • 18
  • 68
  • 98
205
votes
8 answers

How can I return two values from a function in Python?

I would like to return two values from a function in two separate variables. For example: def select_choice(): loop = 1 row = 0 while loop == 1: print('''Choose from the following options?: 1. Row 1 …
Lycon
  • 2,271
  • 2
  • 13
  • 15
200
votes
14 answers

How to return 2 values from a Java method?

I am trying to return 2 values from a Java method but I get these errors. Here is my code: // Method code public static int something(){ int number1 = 1; int number2 = 2; return number1, number2; } // Main method code public static…
javaLearner.java
  • 2,151
  • 2
  • 16
  • 12
167
votes
14 answers

Android ACTION_IMAGE_CAPTURE Intent

We are trying to use the native camera app to let the user take a new picture. It works just fine if we leave out the EXTRA_OUTPUT extra and returns the small Bitmap image. However, if we putExtra(EXTRA_OUTPUT,...) on the intent before starting it,…
Drew
  • 1,719
  • 4
  • 12
  • 5
159
votes
8 answers

Is it good style to explicitly return in Ruby?

Coming from a Python background, where there is always a "right way to do it" (a "Pythonic" way) when it comes to style, I'm wondering if the same exists for Ruby. I've been using my own style guidelines but I'm thinking about releasing my source…
Sasha Chedygov
  • 116,670
  • 26
  • 98
  • 110
1
2 3
99 100