When we call a function, memory is allocated for variables within the function.
These variables are available:
• only within this function
•
functions called inside this function....
Terminology:
Variables within a function are locally scoped.
When the function terminates, the memory for the variables is freed up.
Attention! Memory is not released if a variable inside a function is then used:
• a global variable refers to a variable within a function
• the variable inside the function is returned
return
JavaScript
<html>
<body>
<script>
// declare a function
function ShowValue()
{
var a = "Hello";
var b = "Good morning";
// show the values on the screen
alert(a);
alert(b);
// function ends
return b;
}
// call the function
ShowValue();
</script>
</body>
</html>