views:

24

answers:

2

Should functions always return something? I often write very basic functions which are used as shorthand to do things that happen a lot such as:

function formsAway() {
    $("#login_form, #booking_form").slideUp();
}

Should this function exist - is there a better way, or is this fine?

+2  A: 

They don't have to return anything. If you leave it blank it simply returns 'undefined' which in this case is fine because you never intend to use the return value. The Javascript syntax is pretty simplistic and as far as I know there just isn't any real distinction between functions that do and functions that don't return a value (other than the 'return' keyword)

nukefusion
A: 

Should functions always return something?

I believe that totally depends on the usage of the function.

is there a better way, or is this fine?

IMO writing these kind of functions is good when there are many occurrences of the reusable code which can be replaced by the function so that the code looks much cleaner. But only for a couple of occurrences you may just put the code as it is instead of replacing it with function. You should also take into account the chances of reuse of this function in other places in future.

Gopi