Jump to content

return command in functions


crazyswede

Recommended Posts

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

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...