andrewducker: (Default)
andrewducker ([personal profile] andrewducker) wrote2010-02-11 03:11 pm

Thinking about code

  If I have a method:
int DoSomething(string someStuff)
{
  //Do Stuff
  return 42;
}
then effectively I have an unnamed variable that gets set by the return statement, yes?

That being the case, wouldn't it be in some ways clearer to have an explicit, named, variable that gets set instead?

int DoSomething(string someStuff)
{
  //Do Stuff
  returnValue = 42;
}

where "returnValue" is a keyword that's used to return the value.

As it is I frequently end up with code that creates (or sets) a variable at various points through the code just so it can be returned at the end.  Making this an explicit part of the language just makes sense to me.

I assume there are languages out there that do this.

[identity profile] bracknellexile.livejournal.com 2010-02-11 03:18 pm (UTC)(link)
VB lets you do both in that you can have:

Function DoSomething(someStuff as String) as Integer
  'Do Stuff
  Return 42
End Function

or:

Function DoSomething(someStuff as String) as Integer
  'Do Stuff
  DoSomething = 42
End Function

where it lets you use the function name as a variable of the same type as your explicit named variable that will be returned.
Edited 2010-02-11 15:19 (UTC)

[identity profile] momentsmusicaux.livejournal.com 2010-02-11 03:42 pm (UTC)(link)
I can't decide whether that's silly or perlish.

Ah. Yes. It's silly because if you set $return halfway down your function, you can't tell at the end that you are returning something. Potentially hard to work out what is going on.

I'm not sure what you mean by an unnamed variable being set though... maybe that's a C# thing?

[identity profile] skington.livejournal.com 2010-02-11 03:43 pm (UTC)(link)
You don't have an unnamed variable. You have, in this case, a constant that is passed back to the caller, without any memory allocation.

Now, having syntactic sugar where saying functionname = might be useful in some cases, although the fact that it looks and behaves like a normal variable, but has special behaviour based on the fact that it's called the same thing as the function, is something that would make me uncomfortable: you have to remember to update that variable name if you change the function name, or cut and paste some code from one function to another.

Also, if your function doesn't have a name (in the case of anonymous functions and/or closures), you can't do that. So you have to fall back to the far more obvious and readable return statement.

I think if you've got a number of different return values, that you'll decide according to various criteria, scattered through the code of the function, and you then finally return the value after having done all your processing, then it makes sense to declare a return variable before all of your ifs and switches etc. and then return it.

But I don't see why you'd want the overhead of doing that in all cases.

(I'm coming at this from a Perl, and occasionally Javascript, background, FWIW.)

[identity profile] nmg.livejournal.com 2010-02-11 04:04 pm (UTC)(link)
Lisp and Scheme do something orthogonal, but related; the value returned by a lambda expression is the value of the last expression evaluated in it.

For example (in Scheme), if we have a function:

(define fib
  (let ((ult 0)(pen 1))
    (lambda ()
      (let ((tmp pen))
        (set! pen (+ ult pen))
        (set! ult tmp)
       ult))))


Each time fib is called, the value returned is that of ult (set! returns an unspecified value).

If we wrote this in Common Lisp instead (where setq returns the value used to set the variable) we could write this instead:

(let ((ult 0) (pen 1))
  (defun fib ()
    (let ((tmp pen))
      (setq pen (+ ult pen))
      (setq ult tmp))))


Here, the value returned by fib is the value of the final setq, which is the value of tmp.
Edited 2010-02-11 16:05 (UTC)
simont: A picture of me in 2016 (Default)

[personal profile] simont 2010-02-11 04:13 pm (UTC)(link)
Nobody's mentioned Pascal yet, so: assigning to the name of the function is the way to set the return value in Pascal.

[identity profile] lpetrazickis.livejournal.com 2010-02-11 04:22 pm (UTC)(link)
I prefer having multiple return statements. Languages that have this sort of a variable also tend to not allow for early function termination, forcing you to wrap your code in layers of conditionals.

Languages with a return variable:

function monkeys() {
if (foobar) {
return = 42;
} else {
// ...
if (dagnabit) {
return = 13;
} else {
// ...
if (consarnit()) {
return = e^(-i*pi)
} else {
// ...
}
}
}
}
// fall out of function
}


Language with a return statement:

function monkeys() {
if (foobar) return 42;
// ...
if (dagnabit) return 13;
// ...
if (consarnit()) return = e^(-i*pi);
// ...
}


I find the latter preferable.

[identity profile] draconid.livejournal.com 2010-02-11 08:16 pm (UTC)(link)
I'm sure I've used a language whereby, if you don't explicitly return something, it automatically returns whatever the value of the last line/expression is. I have no idea which one that is though!

[identity profile] xraycb.livejournal.com 2010-02-12 12:12 am (UTC)(link)

In matlab you declare the name of your result variable in the function definition:

function outvar=func_name(in1, in2)
outvar = in1 + in2;

multiple results are returned like this:
function [out1 out2]=func2(in1, in2)
out1 = in+1;
out2 = in+2;


In Eiffel, you set the result of a function by assigning to a compiler-created variable called Result.

[identity profile] undeadbydawn.livejournal.com 2010-02-12 04:11 pm (UTC)(link)
I read this thread because code [like Logarithms] is an abject mystery to me. I keep on believing that one day it'll magically make sense.

nope. not today. Brain is goo, again.