crazyswede Posted December 28, 2012 Share Posted December 28, 2012 I’m new to functions. The “return” command was not well explained on your website. What I have found, is variables set in the functionare readily passed back to the program when the function ends, so why would I ever need to use return? Link to comment Share on other sites More sharing options...
davej Posted December 28, 2012 Share Posted December 28, 2012 (edited) Globals would be visible in the calling function. One function I commonly use is: function $(id) {return document.getElementById(id);} Edited December 28, 2012 by davej Link to comment Share on other sites More sharing options...
Ingolme Posted December 29, 2012 Share Posted December 29, 2012 Return is what the function gives back to the caller.When you call a function and assign it to a variable the return value is what the variable will contain. function sum(a, { return (a + ;}var X = sum(5, 3);alert(X) // Displays "8" Link to comment Share on other sites More sharing options...
jeffman Posted December 29, 2012 Share Posted December 29, 2012 If you create variables within a function, but don't use the var keyword, those variables will be globally visible. Example: function foo () { x = 5;}alert (x); // alerts "5" Usually this practice is discouraged because it is hard to keep track of globals created this way. The following is better: function foo () { var x = 5; return x;}alert (foo() ); // alerts "5" Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now