-8

I have functions that use returned variables.

How do I call them in a main function like this:

    text = 'a'
    def a(text):
        text = x
        return x
    
    def b(x):
        x = z
        return z

    def main():
        # What do I put here to call function a and b?
    
    main()
Lime
  • 28
  • 5

3 Answers3

0

How do I call them

You already know how to call them ;)

def main():
...
main()

So I assume you are bewildered by:

def a(text):
text = x
return x

That x is not defined in a nor is it passed by argument to it. But that's OK. In python, all variables defined outside function are still visible inside it. Only exception is if argument of function have the same name as one of those variables. Then argument will take precedence.

This mechanism is called closure. Function close over some variable that already exists in scope when function is defined.

You can learn about it some more here: https://www.learnpython.org/en/Closures

przemo_li
  • 3,932
  • 4
  • 35
  • 60
0
text = 'a'
def a(text):
    text = x
    return x

def b(x):
    x = z
    return z

def main():
    x = a(text)    
    z = b(x)
main()

This might be what you want

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Andrii Chumakov
  • 279
  • 3
  • 12
0

It is not clear what are you trying to achieve. You are using variable x in the function a and variable z in function b without defining these two variables.

Moreover, you are setting variable text in function a without capturing it using statement global, look here for more information.

And global variables are considered to be bad practice.

Lime
  • 28
  • 5