Value Returning Functions
EasyProgramming.net tutorial on value returning functions
by Nazmus Nasir
HTML
<!-- Easy JavaScript # 29 - Self-invoking Functions -->
<p>
Welcome to the 29th Easy JavaScript tutorial, part of <a href="http://www.easyprogramming.net">EasyProgramming.net</a>. We continue to look at JavaScript functions by working with self-invoking functions.
</p>
<p>
Self-invoking functions are exactly what they sound like, functions that invoke themselves. Unless you loop through them, they usually just run once per script.
</p>
<h2>
Syntax of a self-invoking function:</h2>
<p>
<code><pre>
(function(){
//declare local variables
//run this piece of code
//invote it by using ();
})();</pre>
</code>
</p>
<p>
<h2>
Let's practice:</h2>
You borrowed $100 with an interest rate of 5%.<br />
The total owed with interest is: $<span id="total">___</span>
<br /><br />
<!-- You also borrowed $500.<br />
The total owed with interest is: <span id="2nd">___</span>
</p> -->
JavaScript
var loan = 100;
var interest = 0.05;
(function(x,y){
var calculated = x + (x * y);
document.getElementById("total").innerHTML = calculated;
}
)(loan,interest);