Why not allow empty code blocks?

Chris Angelico rosuav at gmail.com
Sun Jul 31 00:21:16 EDT 2016


On Sun, Jul 31, 2016 at 1:51 PM, Random832 <random832 at fastmail.com> wrote:
> On Sat, Jul 30, 2016, at 22:17, Chris Angelico wrote:
>> Yeah. The distinction means you have a fundamental API difference
>> between the procedures (which don't return anything) and the functions
>> (whose return values you mightn't care about).
>
> Not really any more than between functions returning different types -
> which python doesn't distinguish in general. That some languages may
> spell it with a different keyword rather than a special type [such as
> C's void] doesn't mean the fundamental API difference is of a different
> kind.

In C, you can treat any function as a procedure by simply ignoring its
return value, as in Python. So all functions, whether they return int
or float or void, are functions - there's no fundamental difference.
What I'm talking about is having two types of callables:

a = function(x, y)
call procedure x, y

REXX has this distinction at the call site (because of other API
considerations regarding loose expressions), but it's not fundamental;
the second form will ignore any return value, ergo a procedure can be
upgraded to a function without breaking anything. Python has no
distinction whatsoever, requiring all functions to return *something*,
even if it's None. Pike has a concept of void functions, but deep in
the internals, they just return zero, so if you mess with the type
checker and get around the compile-time checks, you'll get zero back.
C... I dunno for sure, and it probably depends on the compiler, but I
just tried it on my gcc, and it looks like it basically takes whatever
happened to be sitting in the accumulator. In all cases, anyone who
wasn't expecting a return value will be fine with a version of the
function that returns something.

ChrisA



More information about the Python-list mailing list