Computer Science, asked by soniaravind3, 7 months ago

5. Wha happens if you modify a variable outside the function? Give an example.​

Answers

Answered by ayushbelwal61
3

Explanation:

The reason you can not use the variable outside of the function is because of the variable scope, meaning if it is declared locally inside the function that is the only place you can use it. There are a couple of solutions depending on what you are trying to do accomplish. If you want to use r anywhere you can declare r globally and then just reassign the value of r inside your function. then you can check r after the function call. Another way is to return r and assign it outside of your function. Examples:

Global Method:

var r;

function myFunction() {

r = Math.random();

}

this would leave r accessible everywhere for better or for worse.

the method to return r for later use or verification would be:

function myFunction() {

var r = Math.random();

return r;

}

var valueOfR = myFunction();

the last technique just returns the value of r from myFunction() and assigns it to the variable valueOfR that was made outside of myFunction(). You could then verify values with valueOfR instead of directly accessing r.

There are probably a whole lot more solutions to your problem but there are 2 of them

Please mark my answer brainliest.

Similar questions