-4

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.

Okinawa Sama
  • 55
  • 1
  • 1
  • 5
  • Please post your complete code that doesn't work, feeding us snippets without the complete context is counter-productive – EdChum Jul 07 '15 at 07:52
  • This part as I posted it is all I needed looked at. It was made purely for practice so it's not actually part of a wider code. – Okinawa Sama Jul 07 '15 at 10:21

8 Answers8

4

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)
Maroun
  • 94,125
  • 30
  • 188
  • 241
  • But surely by returning y we should be able to access the variable by calling fpol(4)? Is there any logical reason why this shouldn't be the case? – Parsa Jul 07 '15 at 07:54
  • 3
    @user2662639 you don't return *the name `y`*, you return *the object referenced by that name*. There are many logical reasons why this should be the case! – jonrsharpe Jul 07 '15 at 07:55
  • Ah, apologies I misinterpreted the question. Yes this is pretty obvious. – Parsa Jul 07 '15 at 07:56
1

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)
Stian Sandve
  • 520
  • 4
  • 13
1

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

I guess you need to store the value returned by the function in a variable and then print it:

y = fpol(4)
print y
ZdaR
  • 22,343
  • 7
  • 66
  • 87
0

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.

MeetTitan
  • 3,383
  • 1
  • 13
  • 26
0

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.

paolov
  • 2,139
  • 1
  • 34
  • 43
0

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.

Martin Evans
  • 45,791
  • 17
  • 81
  • 97
0

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

Nick
  • 9,962
  • 4
  • 42
  • 80