Python Forum
Globals vs Function - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Globals vs Function (/thread-1909.html)



Globals vs Function - mcmxl22 - Feb-04-2017

Is using a function like this to define variables ok? 
def variables(a, b):
    a = something
    b = another

def some_function():
    do something with variables(a).
    do something with variables(b).
What is the difference between using a function to define variables and just using globals like this?
a = something
b = another

def some_function():
    do something with a.
    do something with b.



RE: Globals vs Function - wavic - Feb-04-2017

Hello!
If you want to do something with outer to the function variables it's better to pass them as parameters.

num1 = 44
num2 = 55

def addition(a, b):
    returnt a + b

result = addition(num1, num2)
print(result)
Output:
99
Variables defined within the function are local to this function and exist as long as the function is running.


RE: Globals vs Function - ichabod801 - Feb-04-2017

When you are using global variables in a local scope, you are cross scope boundaries. This can become confusing and hard to track. If you run into a problem with the values of a and b, it can be harder to figure out where a and b are being modified. If you are passing them around as parameters and return values, it becomes easier to track down where they might get modified.