Return doesn't work (python)
Question:
I’ve been having some problems with the return statement and I can’t seem to figure out what’s wrong. It seemed to work fine yesterday, but today no function that contains it seems to work properly. Here’s an example of what’s going on:
def fpol(x):
y=x**4
return(y)
If I then type in
fpol(4)
I’m given the answer 256 (as I would expect). If I then type in
print(y)
or try to use/view y in any way, I’m told
NameError: name 'y' is not defined
I’ve also tried it with return(y) being replaced by return y . I can also insert print(y) into the original function and that works fine, so I know that during the function, y actually does have a value, it’s just not being returned. Any help is much appreciated.
Edit: I’ve now been able to work past the issue I had with the return function. Thanks to everyone who responded.
Answers:
I guess you need to store the value returned by the function in a variable and then print it:
y = fpol(4)
print y
y
is known only in the scope of the function fpol
. You should assign the result to a variable, and only then print its value:
y = fpol(4)
print(y)
Note that y
is a different variable here, it has nothing to do with the y
inside the function. You could write:
x = fpol(4)
print(x)
The variable y
is only visible from within the function you have declared. To print the result of fpol(4)
you can assign the returned value to a new variable:
returnedValue = fpol(4)
print(returnedValue)
y
is out of scope. It was only in scope for your function call, and since the function fpol
has ran and ended, the scope has died with it. We need to assign a variable that’s visible to the print command. Let’s reuse y
for simplicity.
y = fpol(4)
print(y)
The key rule of thumb for python is every time you indent you have started a new scope! You must make sure your variables are in scope to use them.
y
is a variable with scope that’s local to function fpol().
it is not defined outside of that scope.
The code return y
does not make y
visible outside of the function in which it has been defined. It only returns the value of y
to the caller as a function return value.
Using your example, the following would show you the value of y
def fpol(x):
y=x**4
print(y) # This will print the value of y
return(y)
print fpol(4) # This will print the returned result
But trying to print y
after your function call will fail with the not defined error as the variable is defined only inside you function, i.e. it is local to that function. This is often referred to as the scope of a variable.
As soon as the function returns, y
ceases to exist.
y is not defined outside th function
you can not do print(y)
probably you want
y = fpol(4)
print(y)
is not very good programming style, but you also can make y global variable. then it will be available after function, but pls do not do it.
finally you can do just
return y
no need ()
I suspect you are trying to print(y) outside the function. The variable y is local in scope, that is only defined within fpol(). So you can print it there. You can do:,
def fpol(x):
y=x**4
return(y)
y = fpol(4)
print(y)
But not:
def fpol(x):
y=x**4
return(y)
print(y)
I’ve been having some problems with the return statement and I can’t seem to figure out what’s wrong. It seemed to work fine yesterday, but today no function that contains it seems to work properly. Here’s an example of what’s going on:
def fpol(x):
y=x**4
return(y)
If I then type in
fpol(4)
I’m given the answer 256 (as I would expect). If I then type in
print(y)
or try to use/view y in any way, I’m told
NameError: name 'y' is not defined
I’ve also tried it with return(y) being replaced by return y . I can also insert print(y) into the original function and that works fine, so I know that during the function, y actually does have a value, it’s just not being returned. Any help is much appreciated.
Edit: I’ve now been able to work past the issue I had with the return function. Thanks to everyone who responded.
I guess you need to store the value returned by the function in a variable and then print it:
y = fpol(4)
print y
y
is known only in the scope of the function fpol
. You should assign the result to a variable, and only then print its value:
y = fpol(4)
print(y)
Note that y
is a different variable here, it has nothing to do with the y
inside the function. You could write:
x = fpol(4)
print(x)
The variable y
is only visible from within the function you have declared. To print the result of fpol(4)
you can assign the returned value to a new variable:
returnedValue = fpol(4)
print(returnedValue)
y
is out of scope. It was only in scope for your function call, and since the function fpol
has ran and ended, the scope has died with it. We need to assign a variable that’s visible to the print command. Let’s reuse y
for simplicity.
y = fpol(4)
print(y)
The key rule of thumb for python is every time you indent you have started a new scope! You must make sure your variables are in scope to use them.
y
is a variable with scope that’s local to function fpol().
it is not defined outside of that scope.
The code return y
does not make y
visible outside of the function in which it has been defined. It only returns the value of y
to the caller as a function return value.
Using your example, the following would show you the value of y
def fpol(x):
y=x**4
print(y) # This will print the value of y
return(y)
print fpol(4) # This will print the returned result
But trying to print y
after your function call will fail with the not defined error as the variable is defined only inside you function, i.e. it is local to that function. This is often referred to as the scope of a variable.
As soon as the function returns, y
ceases to exist.
y is not defined outside th function
you can not do print(y)
probably you want
y = fpol(4)
print(y)
is not very good programming style, but you also can make y global variable. then it will be available after function, but pls do not do it.
finally you can do just
return y
no need ()
I suspect you are trying to print(y) outside the function. The variable y is local in scope, that is only defined within fpol(). So you can print it there. You can do:,
def fpol(x):
y=x**4
return(y)
y = fpol(4)
print(y)
But not:
def fpol(x):
y=x**4
return(y)
print(y)