Benefits of function prototyping

After half an hour of researching on the internet, I couldn't find a reasoned discussion of the benefits of function prototyping.

I am running in Java / Android and starting a C course. Prototyping looks cumbersome compared to my previous experience and I would like to know the reason (s) why it still exists in 2013.

I understand that life was more difficult for Richie and his friends; however, today a compiler can be made that will generate a list of functions in the first pass, and then do the usual thing using that list of functions, since the current compiler will use the header file.

It probably cannot persist just because of backward compatibility. It would be possible to create a compiler that could switch between the current mode of operation and the hypothetical new mode I just described, depending on the code it is shown.

If prototyping persists, it should therefore interest the programmer and not the compiler programmer. Right or wrong, and where can I find a reasoned discussion of the benefits of prototyping functions and prototypes?

+3


source to share


2 answers


You forget that in C you can call a function that you don't have a source for.

C supports binary distribution of code, which is fairly common for (commercial) libraries.

You get a header that declares the API (all functions and data types) and code in a .lib file (or whatever your platform). This is typical of the entire C standard library; you don't always get the source in the compiler vendor library, but you can still call functions, of course.

To do this, the C compiler must have declarations when processing your code, so it can generate the correct arguments to invoke and of course deal with any return value correctly.

It is not enough to simply rely on your source, as if you do



GRAPHICSAPI_SetColorRGB(1, 1, 1);

      

but the actual declaration is:

void GRAPHICSAPI_SetColorRGB(double red, double green, double blue);

      

the compiler cannot magically convert your arguments int

to double

unless it has a prototype. Of course, having a prototype allows you to check for errors, that the call makes sense, which is very valuable.

+8


source


The interesting idea is that the compiler goes through all source files first to notice all function prototypes.

However

  • libraries (object code) must have their declarations somewhere, that's why include exist


It is also more convenient for me to be able to grep

include as "free text", for example

grep alloc /usr/includes/*

      

+3


source







All Articles