2 ms·
Well, yeah, you can do this in pretty much any language that has delineated scopes like that. Of course, blocks-are-expressions is necessary if you want to ret
by kelnos 4y ago
Well, yeah, you can do this in pretty much any language that has delineated scopes like that.
Of course, blocks-are-expressions is necessary if you want to return the result of your computations from the block and store it in a variable outside the block. (You can of course declare the storage variable outside the block and assign to it inside, but that's less nice.)
GNU C even has an extension called "statement expressions" where blocks can return values; the syntax looks like this:
int foo = ({
int bar = 4 * 3;
bar;
});
Clang implements it in addition to GCC, as do a few other compilers. (Notably, IIRC, MSVC does not.)