AliDeV
AliDeV

Reputation: 213

Passing returned variables between functions

I spent hours trying to figure out how to pass the value to a function that only prints what it receives.

fun1(50)    

def fun1(salary):
       salary_double = salary * salary
       salary_tax = salary 1.04
       return salary, salary_double, salary_tax

def fun2():
       print (salary, salary_double, salary_tax from fun1)

How can I pass the value of the returned variables in the fun1 to fun2?

Upvotes: 1

Views: 34

Answers (2)

developer_hatch
developer_hatch

Reputation: 16224

Let me know if I understood the question here:

def fun1(salary):
       salary_double = salary * salary
       salary_tax = salary * 1.04
       return salary, salary_double, salary_tax

def fun2():
  salary, salary_double, salary_tax = fun1(50)
  print (salary, salary_double, salary_tax)

but you can do directly:

 def fun3():
  for x in fun1(50):
    print(x)

or

def fun3():
  print(fun1(50))

or @Adam's ways:

def fun4():
  print(*fun1(50))

Upvotes: 1

Adam Smith
Adam Smith

Reputation: 54163

just as fun1 needs arguments to work, so does fun2. You can use *args to make this variadic, or specify that it should receive 3 values every time

def fun2(*args):
    print(*args)

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

However at this point you're just duplicating a print call. Do this from fun1 instead.

Upvotes: 3

Related Questions