May-09-2018, 08:43 PM
Hello all - trying to grasp the concept of functions and every time I think I have it, I realize I really don't.
Here are two examples I found online. I can SEE how they're different but the lightbulb hasn't gone off as to why the results are different. I tried visualizing the code in the pythontutor visualization tool but still not getting it. Can someone break this down for me like I'm a 2-year-old?
Example one:

Example one:
# Function definition is here def changeme( mylist ): "This changes a passed list into this function" mylist.append([1,2,3,4]); print "Values inside the function: ", mylist return # Now you can call changeme function mylist = [10,20,30]; changeme( mylist ); print "Values outside the function: ", mylist
Output:Values inside the function: [10, 20, 30, [1, 2, 3, 4]]
Values outside the function: [10, 20, 30, [1, 2, 3, 4]]
# Function definition is here def changeme( mylist ): "This changes a passed list into this function" mylist = [1,2,3,4]; # This would assig new reference in mylist print "Values inside the function: ", mylist return # Now you can call changeme function mylist = [10,20,30]; changeme( mylist ); print "Values outside the function: ", mylist
Output:Values inside the function: [1, 2, 3, 4]
Values outside the function: [10, 20, 30]