-1

I'm trying to do something like this:

def func(varname):
       varname = 1

func(x) 
print(x)

But I get NameError: name 'x' is not defined

qwerti1
  • 3
  • 1
  • 1
  • 4
    you need to go back and reread the section on how variables work and are passed into functions. also, x was never assigned a value therefore you got the error you got. – LhasaDad May 31 '21 at 01:40

3 Answers3

0

You need to declare the thing you are putting into the function as a parameter. You should also actually do something with the value you are changing in the method.

In your example

def func(varname):
       varname = 1
       return varname #Return whatever you do to the input
x=3
x =func(x) #Do something with the value returned from your method 
print(x)
StarshipladDev
  • 1,166
  • 4
  • 17
  • you have done more than define the value, you also have reassigned the value with the result of the function. This is not much of an explanation of what is going on for the person asking the question. – LhasaDad May 31 '21 at 01:42
  • 1
    @LhasaDad good point, I've edited as such – StarshipladDev May 31 '21 at 01:44
0

No, the syntactical construct you seem to be interested in is not possible. The reason is that function parameters are copies of the value (or reference) of the variable you pass in. Changing the value (or reference) of varname cannot change the value (or reference, for mutables) of x.

Now, the behavior that you want is totally possible. As a general rule, in order to have a function create a value and then assign that value to a variable name of your choice you use a return statement:

def funcname():
    return 1

x = funcname()
print(x)
Him
  • 5,257
  • 3
  • 26
  • 83
  • Python does not copy the values passed to functions. i.e. python does **not** use call by value as an evaluation strategy, or for that matter, call by reference. The type of the object is totally irrelevant too. – juanpa.arrivillaga May 31 '21 at 02:41
  • @juanpa.arrivillaga, sure, but all of that has been intentionally absracted away from the programmer. From the programmer's POV, python had might as well be copying the value rather than copying the object reference of an immutable object. If you think that explaining pass-by-object-reference to qwerti1 is suitable at this stage of their python education, please do post an answer. – Him May 31 '21 at 02:56
  • it is absolutely worth *not* saying things that are false, like Python copies the values you pass in to the function. And the fact that there is some PyObject pointer that is copied is a CPython implementation detail. The evaluation strategy python uses is not abstracted away at all, *it is the semantics of the abstraction* – juanpa.arrivillaga May 31 '21 at 02:59
  • The idea that saying some strange, complicated untruth is somehow less confusing than the rather simple truth is highly suspect. It is rather straightforward to explain that assigning to some local variable will not affect another variable in the callers scope. Mutability is totally irrelevant here and mentioning it is only confusing – juanpa.arrivillaga May 31 '21 at 03:01
  • @juanpa.arrivillaga :) are you telling me, or the OP? Please post this in an answer. – Him May 31 '21 at 03:05
  • Question got closed, although, it's probably a duplicate of https://stackoverflow.com/questions/986006/how-do-i-pass-a-variable-by-reference anyway – juanpa.arrivillaga May 31 '21 at 03:06
0

Mostly the information you get back from a function is what is given in the return value. It's possible, even common, for functions to make changes in mutable data structures that they are passed (e.g. a list), especially where this is capturing state information or updating on the basis of other information handled by the function. And if you rearrange the contents of a list, the list elements will certainly have different values when the function exits.

Certainly you could do this:

def square_it(varname):
    return varname*varname

x = square_it(3)
print(x)

giving output of 9, of course. You can also assign x to something else so that they now both have the same value

y = x 
x = square_it(y)

which in some senses "changes the name" of what is referring to the value 9 to y, and moves x on to refer to something else.

Joffan
  • 1,485
  • 1
  • 13
  • 18
  • I don't know if it is *common*. Indeed, I would say functions that mutate their inputs should generally be discouraged, and considered and anti-pattern – juanpa.arrivillaga May 31 '21 at 02:45