Excerpt from book Clean Code authored by Robert Martin:
In the early days of programming we composed our systems of routines and subroutines. Then, in the era of Fortran and PL/1 we composed our systems of programs, subprograms, and functions. Nowadays only the function survives from those early days.
In my opinion, this excerpt is telling that routine is a bad thing, and is getting out of programming era.
I did some search and found that the difference between subroutine and function in Fortran is:
- Subroutine performs some operation on the input variables, and as a result of calling the subroutine, the input variables are modified.
- Function takes one or many parameters as inputs and returns a single output value.
Although I didn't write Fortran and only write C, there is a same construct supported by C as in int foo(int x)
and void foo(int* x)
.
What drew my attention is that I never heard of void foo(int* x)
is a bad convention in C, so I want to figure out what does Robert Martin mean by this excerpt.
One more question. From OOP's perspective, what if I want to simulate OOP in C, is routine a good choice over function?
It is basically redundant to have a separate language construct once you allow the function to return nothing or also discarding the function result. A C or C++
void
function is really like a subroutine, it just looks like a function and uses the same keyword. So you save the need of one keyword and the language is more unified. And C is the origin of syntax of most languages used in practice today.