Returning multiple outputs from function

how to return multiple values from a function in c
how to return multiple values from a function in c++
python call function multiple return values
return multiple values python function
python function return list
how to return multiple values from a function in java
multiple return statements in c
c++ multiple return values c++17

I am not sure that this is possible, but...

I am trying to write a function that will output lists of numbers so that they can be input into another function. I'm at a dead end though, as neither return or yield (used in this context) can give me what I want.

def ITERATOR():
    for number in range(1,3):
        for item in itertools.permutations(range(15),number):
            yield item

Obviously, the function breaks whenever return is used, and yield returns everything at once. What I ideally want it to output is what is printed on each line by:

def ITERATOR():
    for number in range(2,4):
        for item in itertools.permutations(range(15),number):
            print(item)

i.e:

(0, 1)
(0, 2)
(0, 3)
(0, 4)
(0, 5)
(0, 6)
(0, 7)
(0, 8)
(0, 9)
(0, 10)
(0, 11)
(0, 12)
(0, 13)
(0, 14)
etc...

Is there any way of achieving this?

If you want to return a list, you could just do it:

def fct():
    return [item for item in itertools.permutations(range(15),number) for number in range(2,4)]

And for a generator

def fct():
    return (item for item in itertools.permutations(range(15),number) for number in range(2,4))

Returning Multiple Values in Python, In Python, we can return multiple values from a function. Output: geeksforgeeks 20. 3) Using a list: A list is like an array of items created using square brackets. 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 problematic as the number of values returned increases. What if you want to return four or five values?

Solution

new_tuples = []
for i in range(0, 5):
    for j in range(1, 16):
        new_tuples.append((i, j))

for i in new_tuples:
    print(i)

Output

(xenial)vash@localhost:~/python$ python3 text_seg.py 
(0, 1)
(0, 2)
(0, 3)
(0, 4)
(0, 5)
(0, 6)
(0, 7)
(0, 8)
(0, 9)
(0, 10)
(0, 11)
(0, 12)
(0, 13)
(0, 14)
(0, 15)
(1, 1)

Comments

Would this approach satisfy your needs? You could interchange the numbers as desired but this, will allow you to have a list with the tuples store and be able to call them to print.

How to return multiple values from a function in C or C++? , Below are the methods to return multiple values from a function in C: Output: Enter two numbers: 5 8 The greater number is 8 and the smaller number is 5. We can use pointers in C to return more than one value from the function by passing pointers as function parameters and use them to set multiple values, which will then have visibility in the caller function.

You can return the list through the argument of the function, and use the return of the function to indicate the number of permutations founded:

import itertools

permut = []

def ITERATOR(item):
    n_permuts= 0
    for number in range(2,4):
        for item in itertools.permutations(range(15),number):
            permut.append(item)
            n_permuts+= 1
    return n_permuts

print (ITERATOR(permut))
for item in permut:
    print(item)

How to return multiple values from a function in Python, Returning multiple values from a function is quite cumbersome in C and other languages, but it is very easy to do with Python.See the following  How to return multiple values from a VBA function (Part 1) Part 1: Using By Reference argument, collection, and dictionary object. VBA functions (and other programming languages as well) can only return one value. It's a common and useful practice that we need to return more values from a function.

Returning Multiple Values from Functions in C++, Multiple Return Values in C++ with Output Parameters. Back to C++, let's start our quest with the oldest and possibly still most common method  rather than returning multiple values,just return one of them and make a reference of others in the required function for eg: int divide(int a,int b,int quo,int &rem)

Returning multiple values from a C++ function, With introduction of structured binding in C++ 17, returning std::tuple should and an "out door" to a function -- all the inputs go in here, all the outputs come out​  Unlike many other languages, R functions don't return multiple objects in the strict sense. The most general way to handle this is to return a list object. So if you have an integer foo and a vector of strings bar in your function, you could create a list that combines these items: Then return this list.

How to return multiple values from a function in C++17, However, with the introduction of structured binding in C++17, the process is greatly simplified as the output parameters no longer need to be initialized. svg viewer  Reference parameters also known as “ref” parameters are one of the most common and oldest way of returning multiple values from a function. As the name suggests, they are passed as reference to the function and after the function executes, the updated value of the passed reference variable is returned back to the calling method.

Comments
  • Don't put return inside a for loop. It immediately breaks out of the function. Also, ALL_CAPS should really be used for global variable names, not function names: see PEP8
  • "yield returns everything at once" - that's exactly not what it does. Please clarify the specific problem; how are you trying to use that function?
  • yield return a generator to the values it returns, you can use this generator as you wish. you don't have to accept all values at once, just iterate over it.
  • I have another function that outputs when a list (or tuple) of numbers is passed to it. I would like to pass each of these (0,1),(0,2),etc... to it individually. Now that I think about it, would it be possible to do: for item in itertools.permutations(range(15),number): functionTwo(item) ?
  • Just some correctiony to your statements: "neither return or 'yield' (used in this context) can give me what I want" - both can do what you want, but you have to understand how they work. "yield returns everything at once" - it definitely does not.