+ 2

How to return two values from a function?

We know that the function execution terminates after the first return expression, then how can I return two values?

18th Jun 2017, 5:57 AM
Mohit
Mohit - avatar
6 ответов
+ 7
To solve this problem you can return a new instance of a static class with several fields.
18th Jun 2017, 6:56 AM
Alexey
Alexey - avatar
+ 2
You can return the array of values. Any number of one type values. I have finished just this example in "My codes" ;)
18th Jun 2017, 6:53 AM
Sergey Dobrynin
Sergey Dobrynin - avatar
+ 1
But how to return array please send a snippet
18th Jun 2017, 6:55 AM
Mohit
Mohit - avatar
+ 1
Just one step separates you from example code in my profile, but it is no problem: public class Solution { public static void main(String[] args) throws Exception { String names[] = getnames(); System.out.println(names[0]+" + "+names[1]+" + "+names[2]+" = Object-oriented programming!"); } public static String[] getnames() throws Exception { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String[] name = new String[3]; for (int i=0;i<3;i++) { name[i]=reader.readLine(); } return name; } }
18th Jun 2017, 7:00 AM
Sergey Dobrynin
Sergey Dobrynin - avatar
+ 1
Thank you very much
18th Jun 2017, 7:27 AM
Mohit
Mohit - avatar
0
you can't.
18th Jun 2017, 6:15 AM
Edi Lipovac
Edi Lipovac - avatar