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 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)