0

I don't understand how the variables being passed aren't changing when I run the function. The values are initially set to None, as I hope for them to change once I run the function but nothing happens. So I'm obviously doing something wrong. I'm fairly new to Python and would love some help.

def askUser(number_STRING, digits, array_NUM, chars):

  print ("NOTE: Program can only convert mm, cm, m, and km")
  number_STRING = raw_input("Type the number (9KM, 30M exactly like 
  that) you would like to convert to centimeters.")
  digits = int(filter(str.isdigit, number_STRING))
  array_NUM = len([c for c in number_STRING if c.isdigit()])
  chars = str(number_STRING[array_NUM:])

def main():

  number_STRING = None
  digits = None
  array_NUM = None
  chars = None

  askUser(number_STRING, digits, array_NUM, chars)

  print chars
  print array_NUM
  print digits

main()
EthanR
  • 139
  • 9
  • The variables are assigned only within the function. But the function doesn't return anything. – trotta May 17 '17 at 14:29
  • Python function arguments are passed by value, not by reference. Search for `python pass by value` for explanation. – khelwood May 17 '17 at 14:30

1 Answers1

1

Within a function, the variables are treatet as local variables. To declare global variable use the global syntax

e.g.

def a(b):
    global c
    c = c + b
товіаѕ
  • 2,881
  • 4
  • 23
  • 53
  • Thanks! So what is even the point of arguments? I feel like I've completely misinterpreted their use. – EthanR May 17 '17 at 14:34
  • If it helped I'd be thankful if you'd mark my answer as accepted. regarding your quesiton; it is just like in any other programming language. parameters are used to allow a certain degree of generic implementation. lets say if you have a method/function `openLink()` vs `openLink(link)` .. if you just access a global variable it is not very handy and maintainable in the future. passing the link as a parameter guarantees that the correct value is processed in the function scope – товіаѕ May 17 '17 at 14:46