Gabriel Moreno
Gabriel Moreno

Reputation: 61

How passing an argument to a function

Why doesn't this work? What would it take for this to work?

call = 'Duster'
def text(call):
    print(call)

text()

Upvotes: 0

Views: 36

Answers (2)

Alejandro C.
Alejandro C.

Reputation: 3801

It doesn't work because the argument named call takes precedence over the variable named call due to scoping.

You could make this work by using your code and just changing the last line

text(call)

Or you could use the variable directly and not an argument

call = 'Duster'
def text():
    print(call)

text()

Upvotes: 1

Prune
Prune

Reputation: 77827

The call inside your function and call outside your function are utterly independent variables. You have to pass things through the parameter list.

call = 'Duster'
def text(call):
    print(call)

text(call)

Actually, you can use a global variable, but please avoid those.

To illustrate this better, move the lines of your main program together and change the names:

def text(phrase):
    print(phrase)

name = 'Duster'
text(name)

Also, the main program of two lines could be just one line:

text('Duster')

Upvotes: 4

Related Questions