2

There's a bunch of languages that automatically return the last value in a function (mostly functional) like Ruby, Haskell, Lisp, etc.

Does this feature (or what do you call it) affect the performance of these languages?

Martijn Pieters
  • 14,499
  • 10
  • 57
  • 58
UncleLaz
  • 129
  • 1
  • What is the last value? In these languages shouldn't the functions will have only one value? – Manoj R Nov 27 '12 at 08:26
  • @Manoj R: In Scala (and, AFAIK, in Ruby) a block (and, in particular, the body of a function) consists of a sequence of expressions. The return value of a block is the result of the last expression in the sequence. This might be valid for Lisp as well, but I do not know enough Lisp to confirm it. – Giorgio Nov 27 '12 at 09:17
  • 7
    It doesn't *quite* work that way in Haskell: A function never contains more than one expression, and its value is the return value. You may be thinking about monadic computations, where the last action in a `do` block becomes the return value, but if you desugar the `do` notation, you'll see that the entire `do` block is still one expression, and its value (the composed action) is the return value. – tdammers Nov 27 '12 at 09:21
  • Haskell FTW. :) – Manoj R Nov 27 '12 at 10:29
  • The class of languages that do this are [Expression-oriented programming language](http://en.wikipedia.org/wiki/Expression-oriented_programming_languages) –  Nov 22 '13 at 03:24
  • It might encourage to write single expression functions or write functions in a way that allows tail call optimizations… However, it is not like requiring to write a `return` statement prevents that, it is a best a nudge to the developer. On the other hand, some language designers might just be borrowing from other languages because it appeals to their developer base. – Theraot May 30 '20 at 20:45

1 Answers1

4

In a word - no

With a compiler the emitted code would probably be identically anyway, with an interpreter it will be more or less be the same, if anything slightly quicker.

jk.
  • 10,216
  • 1
  • 33
  • 43
  • of course for a language where you can either use the value of the last expression or use return like TCL you could in theory write an implementation where return was much worse, but only as a pathological case. – jk. Nov 27 '12 at 10:33