Re: Pascal vs C
Posted: Thu Sep 03, 2015 12:21 am
Funny how everyone missed my new big point of the functions/procedures thing in Pascal. In my opinion it is far more elegant to abstract something than to break it down into subtypes to the point where the relationship between then is lost - that's how functions and procedures are in Pascal, where a subroutine has to be given a different name just because it happens to need to return a value e.g. an error code.
In C, "int open_file(char *filename);" and "void close_file(int file_descriptor);" can be both referred to as "procedures" to handle files or "functions" to handle files - the terms are interchangable. The code is consistent and the documentation can be consistent as well.
In Pascal, one would have to be "Function open_file(filename:Char):Integer" (might have the syntax slightly wrong there, but the important part is the "Function" keyword) and the other would be "Procedure close_file(file_descriptor:Integer)". So while these are both logically in the same category, as subroutines to handle files, the code and the documentation are both forced to be inconsistent not because of the meaning of the functions but simply because one needs to return a file descriptor and the other one doesn't.
Another thing that results from this, too, is that in C the return value of a function can be dumped, whereas in Pascal a "function" is required to have somewhere to return its value, meaning something like some of the C standard library functions (such as "strcpy" which returns the destination pointer as a return value of the function - probably to allow one to write "new_string = strcpy(malloc(strlen(old_string) + 1), old_string);" which is very useful, by the way) would be really painful to use or something which returns an error code which isn't really needed while we're just getting the basics written (we'll add the error handlers later).
To me these are both serious flaws.
In C, "int open_file(char *filename);" and "void close_file(int file_descriptor);" can be both referred to as "procedures" to handle files or "functions" to handle files - the terms are interchangable. The code is consistent and the documentation can be consistent as well.
In Pascal, one would have to be "Function open_file(filename:Char):Integer" (might have the syntax slightly wrong there, but the important part is the "Function" keyword) and the other would be "Procedure close_file(file_descriptor:Integer)". So while these are both logically in the same category, as subroutines to handle files, the code and the documentation are both forced to be inconsistent not because of the meaning of the functions but simply because one needs to return a file descriptor and the other one doesn't.
Another thing that results from this, too, is that in C the return value of a function can be dumped, whereas in Pascal a "function" is required to have somewhere to return its value, meaning something like some of the C standard library functions (such as "strcpy" which returns the destination pointer as a return value of the function - probably to allow one to write "new_string = strcpy(malloc(strlen(old_string) + 1), old_string);" which is very useful, by the way) would be really painful to use or something which returns an error code which isn't really needed while we're just getting the basics written (we'll add the error handlers later).
To me these are both serious flaws.