2

I am learning python and am currently writing a simple program that has to be divided into functions. My problem is that I have one function that should return strings for four different variables, that then should be used in another function.

E.g.

def function1():
   var1 = input("Write something: ")
   var2 = input("Write something: ")
   var3 = input("Write something: ")

def function2():
   print(var1)
   print(var2)
   print(var3)

function1()
function2()

This gives an error message since var1 is not defined in the frame of function2. How should this be solved? The illustration is very simplified for clarity, but I could post something more specific if required.

David Hasselberg
  • 171
  • 1
  • 2
  • 13
  • 1
    @AvinashRaj really, we shouldn't be teaching people to use globals, if it can be avoided. I'm a bit surprised to see *you*, of all people, do that. Are you having a sarcastic day? – Marcus Müller Feb 08 '16 at 13:01
  • 1
    @David *`Return`* a list, dictionary or tuple of three *values* and pass them as three *arguments* into the second function... – deceze Feb 08 '16 at 13:01
  • This is really extremely basic. You're better off studying some basic python (or any programming language that uses functions, for that matter.) – juanchopanza Feb 08 '16 at 13:03

4 Answers4

6

Return the variables in function1:

def function1():
    var1 = input("Write something: ")
    var2 = input("Write something: ")
    var3 = input("Write something: ")
    return var1, var2, var3

and make them arguments in function2:

def function2(var1, var2, var3):
    print(var1)
    print(var2)
    print(var3)

Call them like this:

var1, var2, var3 = function1()
function2(var1, var2, var3)
Mike Müller
  • 82,630
  • 20
  • 166
  • 161
1

That's not what functions are for.

There is a thing called scoping, which basically says that variables declared within a function are local to that function and can't be accessed by anything else. If you need to pass values to a function, use parameters.

This should all be covered by the Python introduction you're probably currently reading -- just read on one or two pages :)

Marcus Müller
  • 34,677
  • 4
  • 53
  • 94
  • The point of using functions in this exercise was to break down the program into smaller sub-programs. My problem was that in the example in the course the function only needed to return one value, whereas in the exercise you need to return 4 different values using almost the same function (meaning creating 4 different functions would kind of remove the point of having functions since nothing would be simplified). The course showed me how to use a return value from a function, but not if it was possible to have several return functions so that's why I was wondering. – David Hasselberg Feb 08 '16 at 13:15
  • *The point of using functions in this exercise was to break down the program into smaller sub-program* Exactly, and the point of that is to consider your function as isolated thing, which takes input and generates output, but doesn't access the state of the calling function. So write a function that takes `var1`,`var2` and `var3` as parameters! – Marcus Müller Feb 08 '16 at 13:16
1

Correct approach would be to return values from functions and pass them via input arguments:

def function1():
    var1 = input("Write something: ")
    var2 = input("Write something: ")
    var3 = input("Write something: ")
    return var1, var2, var3


def function2(a, b, c):
    print(a)
    print(b)
    print(c)

v1, v2, v3 = function1()
function2(v1, v2, v3)

I renamed some of parameters to emphasize that there is no name relation anywhere. All values are explicitly returned and explicitly passed.

Łukasz Rogalski
  • 22,092
  • 8
  • 59
  • 93
0

With your current example the better approach is reuse the same functions three times:

def function1():
    return input("Write something: ")

def function2(var):
   print(var)

for i in range(0, 3):
    var = function1()
    function2(var)

But it may be better return the variables in function1 and then pass it to function2.

def function1():
   var1 = input("Write something: ")
   var2 = input("Write something: ")
   var3 = input("Write something: ")
   return var1, var2, var3

def function2(var1, var2, var3):
   print(var1)
   print(var2)
   print(var3)

var1, var2, var3 = function1()
function2(var1, var2, var3)

It depends on your specific problem.

k4ppa
  • 4,122
  • 8
  • 26
  • 36