how to make a function have access to a variable outside code example

Example 1: change a variable outside a function js

var global = "Global Variable"; //Define global variable outside of function

function setGlobal(){
       global = "Hello World!";
};
setGlobal();
console.log(global); //This will print out "Hello World"

Example 2: accessing a variable from outside the function in python

# for accessing a variable from outside the function then create that variable
# as an attribute of the function
def hi():
    # other code...
    hi.bye = 42  # Create function attribute.
    sigh = 10

hi()
print(hi.bye)  # -> 42

Tags:

Misc Example