Jump to content

Is it really necesary to have a Return in a function?


Videogamer555

Recommended Posts

What if I want to create a function without a returned value (something that is self contained, rather than generating a value for use in other JScript code)? For example if I have "document.write('test.')" as my the code for my function, its output will be writing this text directly to the browser window. Do I need to have a Return statement just to make it "officially correct"? Or can I leave out the Return statement when the function does not return a value?

 

Currently my code is:

<html><head><title>index</title><script type="text/javascript">function testfunc() {document.open();document.write('test');document.close();}</script></head><body>This is <span onclick="testfunc();">a</span> test.</body></html>

I need to know if I need a "return;" after my "document.close();".

Edited by Videogamer555
Link to comment
Share on other sites

Functions don't have to return a value.

 

If they do not return a value, do they still need a "return" statement to be officially correct? For example:

function MyFunc(){some code that does somethingreturn true;}

Is this required by the JavaScript standard, or not?

Edited by Videogamer555
Link to comment
Share on other sites

In Javascript the return statement is only used if you want to have a returned value. In some cases a boolean value of 'false' is returned by an event handler in order to disable a default action.

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...