Change a variable inside a function from another function, what's the best way?

python variable scope
python call function from another function in class
how to call a function within a function python
how to make a local variable global in python
how to change a global variable in a function python
function inside another function in excel
using variables from other functions python
how to get variable from one function to another in python

Update:

Thanks for the answers and I know putting them in a bigger scope is a good solution. However, what if the two functions are loaded from different external JavaScript files but on the same page?

For instance, the handler of the left button is from a.js and the handler of the second button is from the b.js and b.js is even loaded later when some event triggers it?


I have a function and there's a variable in it. I want to update this variable from another question, what is the best way?

here is the code:

$('#update').click(function(){
  // I want to change the "x" in the other function to a random number or anything else after clicking this.
})
$('#test').click(function(){
  var x = 1;
  a();
  function a(){
    alert(x);
  }
})


// It can be done via the global way, but is that a must approach?

$('#global_update').click(function(){
  window.y = Math.random();
})
$('#global_test').click(function(){
  window.y = window.y ? window.y : 1;
  a();
  function a(){
    alert(y);

  }
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

<button id="test">alert</button>

<button id="update">update</button>

<hr>


<button id="global_test">alert (global)</button>

<button id="global_update">update (global)</button>

If you don't want to pollute the global scope you can wrap both the functions inside an IIFE and declare the shared variabile in the IIFE scope.

(function(){
  var x;
  $('#global_update').click(function(){
    x = Math.random();
  })
  $('#global_test').click(function(){
    x = x ? x : 1;
    a();
    function a(){
      alert(x);

    }
  })
})();

To answer your questions regarding a scenario with two different js files, there's no way you can create a shared common scope between them, in this specific case you have to rely on the global scope.

You can define a sort of subscope of window before importing the scripts and refer to that subscope in the scripts.

<script>
  window.subScopeAB = {
    x: 0
  }
</script>
<script src="a.js"></script>
<script src="b.js"></script>

a.js

(function(){
  window.subScopeAB = window.subScopeAB || {};
  $('#global_update').click(function(){
    window.subScopeAB.x = Math.random();
  })
})();

b.js

(function(){
  window.subScopeAB = window.subScopeAB || {};
  $('#global_update').click(function(){
    window.subScopeAB.x = Math.random();
  })
})();

How to globally change a variable from a function in Python, Edit - I probably should expand on my answer. But with a How do I call a variable from another function in Python? print(“Value Inside function:”,x). x=10. The question is that is there a way to change a value which is defined inside a function file from a script file. For example: I have a function .m file called A, and this function file outputs a struct. Inside this A.m file, I have defined a value x= 5 which is inside a struct along with other variables.

You can't change a local variable in a different scope, and you really shouldn't, since that completely defeats the purpose of variable scopes. If you want to share state via a variable, it has to be in scope for both functions, but that doesn't mean it has to be global. Just define a new scope containing both event handlers, and define the variable in that scope:

(function () {

  var x;

  $('#update').click(function(){
    x = Math.random();
  })
  $('#test').click(function(){
    x || (x = Math.random());
    a();
    function a(){
      alert(x);
    }
  })
})();

Is there a way to pass local variable from function to another function , If you want to modify the values of a global variable inside a function, you must use the global keyword: global x; x = 123; print  Just reference the variable inside the function; no magic, just use it's name. If it's been created globally, then you'll be updating the global variable. You can override this behaviour by declaring it locally using var, but if you don't use var, then a variable name used in a function will be global if that variable has been declared globally.

Instead of making the functions global, make the variable global instead

eg. (in pseudo-code..)

$(function() {
    var global_var;
    function f1 {
       // ...
    }

    function f2 {
       // ....
    }
});

[solved] Use variable from function inside another function, Note that you don't have to assign the same smarty variable in every function unless you want to change its value - it will persist across functions. So I am sure my above is incorrect but you can see how in function 2 I am trying to grab a variable from the first function. Is there a way I can do this? I don't know why, but I keep thinking it has something to do with the event.var1 or the event functionality.

Python Nested Functions, The inner function is able to access the variables within the enclosing scope. What if we attempt to change the variables of the outer function from inside the  Everything in python is considered as object so functions are also objects. So you can use this method as well. You can pass the word variable from the first function 'oneFunction' to the other function. However, I guess you have to put the other function first less you get an error, "anotherFunction is not defined".

Global keyword in Python, Global keyword is a keyword that allows a user to modify a variable outside of the current scope. If a variable is assigned a value anywhere within the function's body, We Use global keyword to use a global variable inside a function. In C programming, functions that use variables must declare those variables — just like the main() function does. In fact, it’s pretty much the same thing. The big difference, which you need to remember, is that variables declared and used within a function are local to that function.

Chapter 15. Functions, The name of a named function expression is only accessible inside the function In other words, a function declaration declares a new variable, creates a function In nonstrict mode, arguments stays up-to-date if you change a parameter: I want to change variable inside function, something simular to this c++ function: void f(int &a){a+=2;} I've heard handles can be useful here, but anything I tried only generated copy of variable, and was unable to change original.

Comments
  • Thanks, Karim, I have updated my question. Would you please take a look?
  • I see. Thanks. Looks fair enough but I am still hoping to find some inspiring tips.
  • maybe the above solution could help you out
  • Thanks, after a long-term research, your way looks quite neat and cool.
  • glad it helped :)
  • Thanks, meagar, I have updated my question. Would you please take a look?
  • Thanks, Rawrplus, I have updated my question. Would you please take a look?