# Function recalling a variable

**URL:** <https://discuss.python.org/t/function-recalling-a-variable/19435>\
**Category:** Python Help\
**Tags:** help, custom-function\
**Created:** [September 25, 2022, 3:23pm UTC](https://discuss.python.org/t/function-recalling-a-variable/19435 "2022-09-25T15:23:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jaywalker061707](https://avatars.discourse-cdn.com/v4/letter/j/c77e96/32.png) [@Jaywalker061707](https://discuss.python.org/u/Jaywalker061707)\
**Post date:** [September 25, 2022, 3:23pm UTC](https://discuss.python.org/t/function-recalling-a-variable/19435/1 "2022-09-25T15:23:58Z")

</div>

def length()  
len(x)

def main():  
x = str(input(“entrer a word:”))  
length()

I get an error message when it runs the length() function saying x is not defined. How can i get a function to pull a user defined variable

---

<div class="post-metadata">

**Author:** ![MRAB](https://avatars.discourse-cdn.com/v4/letter/m/e9c0ed/32.png) [@MRAB](https://discuss.python.org/u/MRAB)\
**Post date:** [September 25, 2022, 3:35pm UTC](https://discuss.python.org/t/function-recalling-a-variable/19435/2 "2022-09-25T15:35:21Z")

</div>

If you bind (assign) to a name in a function, Python will assume that the name is local unless you declare that it’s `global` or `nonlocal`.

In your code, the best solution is to pass the variable into the function through the parameter list.

---

<div class="post-metadata">

**Author:** ![aivarpaalberg](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/aivarpaalberg/32/9457_2.png) [@aivarpaalberg](https://discuss.python.org/u/aivarpaalberg)\
**Post date:** [September 25, 2022, 6:16pm UTC](https://discuss.python.org/t/function-recalling-a-variable/19435/3 "2022-09-25T18:16:40Z")

</div>

You should get SyntaxError not NameError - `length` function doesn’t have colon.

If you solve your current name problem then you will have next one: `length()` function is not that useful. It doesn’t have explicit return so it returns None. Calling this function accomplishes very little.

---

<div class="post-metadata">

**Author:** ![steven.daprano](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/steven.daprano/32/1083_2.png) [@steven.daprano](https://discuss.python.org/u/steven.daprano)\
**Post date:** [September 26, 2022, 1:12am UTC](https://discuss.python.org/t/function-recalling-a-variable/19435/4 "2022-09-26T01:12:54Z")

</div>

In the future, please format your code as code by putting it within  
“code fences”, triple backticks on their own line:

````
```
code goes here
```

````

Triple tildes `~~~` will also work.

Also if you get an error message, please copy and paste the full error, starting with the line “Traceback”, into code fences too. Errors are not always so easy to guess as this one.

In your “main” function `x` is a local variable, so it only exists inside the main function.

One solution is to make it a global variable:

```python
def main():
    global x
    # There is no need to call str(), as input() already returns a string.
    x = input("enter a word:")
    length()

```

A better solution is to pass x as an argument to your “length” function, which will need to be given a parameter to accept the argument:

```python
def length(obj): # Declare that the function needs an argument.
    len(obj)

def main():
    x = input("enter a word:")
    length(x) # Pass x as argument.

main() # Run the main function.

```

If you do that, your input will be called “x” inside the main function, but will have a different name, “obj”, inside the length function.

If you run that code, you will see that main asks you to enter a word, and then apparently nothing further happens. That’s because you calculate the length of the input and do nothing with it, so the interpreter just throws it away. Let’s fix that:

```python
def length(obj): # Declare that the function needs an argument.
    return len(obj)

def main():
    x = input("enter a word:")
    y = length(x) # Pass x as argument, store the returned result as y.
    print("the length of", x, "is", y)

main() # Run the main function.

```
