13

I write JavaScript code and I try to use its functional language nature.

In other functional languages (or even in Ruby), if I don't explicitly set the return value of a function, it will return the value of the last evaluated expression. JavaScript does not follow this pattern. (To be precise, JavaScript always returns a value as well. If nothing was set, then undefined.)

My question is the following: I have a function that doesn't need to (and does not) return a value. Does it make sense in a functional programming context to have a function with no explicit return value? Or did a fail somewhere if I found myself in this case?

For example, I have the following function. It checks periodically if location hash was changed, and if so, calls the given function.

LIB_hashManager = function(f, context) {
    var prev = '';
    var pollHash = function() {
        if (prev !== window.location.hash) {
            prev = window.location.hash;
            f.apply(context);
        }
    };
    window.setInterval(pollHash, 100);
};

Should I return here anything?

Update

Meanwhile it came to my mind, that if anytime in the future I'll need to extend the knowledge of LIB_hashManager, following the functional constructor pattern, I can simply add methods to an object and LIB_hashManager will return that produced object.

LIB_hashManager = function(f, context) {
    // inside logic
    // ...
};

And later I may write:

LIB_hashManager = function(f, context) {
    // inside logic
    // ...

    // return public methods
    return {
        // ...
    }
};

So doesn't make sense then to return an empty object in the first case?

viam0Zah
  • 25,949
  • 8
  • 77
  • 100

6 Answers6

20

A "pure" functional programming environment would have no side effects -- each function's work would be entirely in computing its return value; that's not really feasible in typical uses of Javascript, and so it's perfectly acceptable, when a function has done its work through side effects, to have it return nothing at all, i.e., be a "procedure" rather than a function.

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
  • 17
    As one of the creators of Haskell said (paraphrasing) "If your entire machine ran on the basis of a purely functional approach, then all you'd get out of it would be a hot box, you'd never actually see the results it had computed". :) – Mike Tunnicliffe May 24 '09 at 05:16
  • Damn, wish I could find this quote – rpivovar Jan 18 '20 at 14:15
  • Its one of the basis of the halting problem I believe. Any program either terminate or hangs – user1527152 Jun 29 '22 at 18:17
5

My question is the following: I have a function that doesn't need to (and does not) return a value. Does it make sense in a functional programming context to have a function with no explicit return value? Or did a fail somewhere if I found myself in this case?

According to the academic description of a function: a function must give the same output given an input. A function that does not have any output is absolutely useless, because functions are not supposed to have any side effects.

However, since functional programming languages often need at least 1 side effect, the convention to return nothing is to return a unit or "()". Since this concept does not exist in Javascript, it shouldn't matter to you, since Javascript isn't strongly typed anyway.

Unknown
  • 45,913
  • 27
  • 138
  • 182
2

It is perfectly fine to have a function that does not return anything. In fact, forcing a function that naturally would not have a return value to have one is awkward and smells bad.

onedozenbagels
  • 2,242
  • 2
  • 19
  • 21
  • If it does not have a return value, then it is not a function, even if it is so called in the language we're talking about. – Ingo Dec 16 '13 at 19:05
1

This question has been answered over 4 years ago, but I believe, the accepted answer is wrong


In the code given, author sets interval and then - he does not provide ANY way to stop it.

And so the answer should be: Yes, you should return a value from this function, and that value should be an object allowing you to stop interval, which is started inside of it.

Discussing details of how this should be implemented is out of scope. // you can do it either by returning interval handler, so you can cancel it manually ( see example 1) or by returning an object with method that does that behind the scene (smth like .pause, .stop or .cancel). Alternatively, that object can also allow run-time reconfiguration of hash manager (like changing the interval frequency).

Example 1 (simple):

LIB_hashManager = function(f, context) {
    var prev = '';
    var pollHash = function() {
        if (prev !== window.location.hash) {
            prev = window.location.hash;
            f.apply(context);
        }
    };
    return window.setInterval(pollHash, 100);
};
c69
  • 19,951
  • 7
  • 52
  • 82
1

The function which produces only side effect may be considered simply as an isolated program block, same as procedure block. Since JS doesn't have procedures, there is nothing wrong to use a function as a procedure block. The only exception that functions in JS are objects too, so be careful with extensive use of such 'functions'.

In this case it just increases readability of the program.

Thevs
  • 3,189
  • 2
  • 20
  • 32
0

If you are not supposed to use the result of LIB_hashManager I think you should definetly return undefined (i.e., have no return statement at all).

If you forget this and try to use the result of the function anyway you'll probably just get an error (which is fine since that would be a programming error, a bug!)

Jonas
  • 19,422
  • 10
  • 54
  • 67