Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to acces variables from another function?

How to acces variable from another function?
alert from function 1 takes the sum of the 2 variables from function 2

fun1();

function fun1() {
  alert("var two + var three = " + two + three);
}

function fun2() {
  var one = 1;
  var two = 2;
  var three = 3;
  var four = 4;
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Either define these variables globally outside of both functions (a very bad practice for any real world program – but suitable enough for an example) – or provide them as arguments to the alert function.

var one, two, three, four;

fun1();

function fun1 ()
{
  fun2();
  alert("var two + var three = " + two +  three);
}

function fun2 ()
{
  one = 1;
  two = 2;
  three = 3;
  four = 4;
}
fun2();

function fun1 (one, two, three, four)
{
  alert("var two + var three = " + two +  three);
}

function fun2 ()
{
  fun1(1, 2, 3, 4);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading