Sunday 6 January 2019

C Programming – return of the vanishing parentheses

The other day, an esteemed colleague of mine happened to fulminate against the abominable practice, favoured by some C programmers, of enclosing expressions returned from functions between a pair of parentheses like this:

return (x);

or

return ((x-1) + (y+2));

Now I admit I had previously given no thought to this matter. It is not (I think!) my personal habit to use parentheses in this way but I’ve certainly seen it done and it had never struck me either positively or negatively. After all, what’s a pair of brackets between chums?

But my grumpy colleague was not so forgiving. In his view, the use of round brackets after a return is unnecessary and anyone who does this is guilty of invincible ignorance due to their grievous misunderstanding of the nature of a return statement.

That being so, I wondered why the practice was so commonplace. I did a quick Google search and found about equal numbers of people who agreed and who disagreed with my  grouchy friend. Some said that parentheses around return values are the work of Satan; others claimed that parentheses “add clarity”.

Some say that parentheses are bad is because they are used in the mistaken belief that return acts like a function call. To be honest, this doesn’t sound a very probable explanation to me so I carried on researching.

“What do Kernighan and Ritchie prefer?” I asked myself.

I consulted the second edition of their classic text, ‘The C Programming Language’ and sure enough they don’t use parentheses. Oh well, there are no greater C authorities than K&R, so that surely settles the matter.

Then out of idle curiosity, I decided to check the first edition of ‘The C Programming Language’. If I wore false teeth I might well have swallowed them at this point. To my astonishment, throughout the first edition every code example I could find uses parentheses around return expressions.

They are there in edition one. They are gone in edition two.

But why?

This is what the authors say about return expressions in edition 1 (page 23):
“Any expression may occur within the parentheses”

In edition 2 (page 26) this is rewritten:
“Any expression may follow return”

So why were the parentheses used in edition 1? There may be another clue. The text of that book also states that a value is returned “just as in PL/I”. Now, I have never used the PL/I programming language but it is my understanding that in PL/I it is mandatory to enclose returned expressions between parentheses. Possibly Kernighan and Ritchie initially thought this was a good stylistic convention but then subsequently changed their minds. Or possibly very early C compilers required parentheses but later didn’t.

One thing that is certain is that the immediate predecessor to C, that’s the B language, did require parentheses. ‘The Programming Language B’ manual states:
“A return in a function causes an immediate return to the calling program. If the return is followed by an expression in parentheses, this expression is evaluated, and the value returned to the caller.”

One of the authors of the B manual, incidentally, was the same B. W. Kernighan who later co-authored the famous C book.

I don’t think I’ve absolutely solved the mystery of the vanishing parentheses. What I can say is that in some languages such as PL/I and B, parentheses around return expressions are required. In C they were initially considered to be acceptable (and possibly obligatory?) but they are no longer needed. In spite of that fact, a substantial number of programmers of C and C-like languages such as C# and Java, continue to use them.

So in brief: parentheses around return expressions are not needed.

But if you happen to like them and if you want use them I won’t hold it against you. On the other hand, I have a very grumpy colleague who certainly will….