Prototyping Functions

I’m curious to know what the programmers here think of prototyping functions. It was how I was trained to write C and C++, formally, but I also agree with the reasoning of it.

Since program execution starts in main(), it should be the first function you see, yeah? If you have your functions declared and written, it would take quite some time to scroll through the file to find the main function and see what’s going on.

However, as I’m starting to look at C and C++ code in the wild and written professionally, my professor and I seem to be in the minority. Is my anecdote wrong, and I’m just a newbie that hasn’t gotten in the trenches with these beautiful languages yet? Or is prototyping a beginning tactic?

What do you think? Do you prototype functions? Why or why not?

For those that are not familiar with what prototyping is, here are two examples:

Prototyping

void print_hello();

int main() {
    print_hello();

    return 0;
}

void print_hello() {
    printf("Hello!\n");
}

Not Prototyping

void print_hello() {
    printf("Hello!\n");
}

int main() {
    print_hello();

    return 0;
}

2 Likes

Using header files will help clean this up a bit. I’m still an amateur and haven’t found a reason yet to declare any functions before hand. Also my programs are smol.

4 Likes

This is something I’ve seen a lot, too. That’s a valid point, though. Hadn’t considered the reasoning.

2 Likes

Maybe its to satisfy the compiler when there’s many files and includes to external libraries or something to that nature.
Interested to find out for sure though.

1 Like

Curiosity kills me.

This makes sense. From random on the internet.

No, declaring a function after main requires a function prototype which tells the compiler the return type, name, and passed in values.

A function prototype is this:
();

When the actual function is called the compiler already knows what it needs to know to call the function.
Defining the whole function body before main doesn’t require a function prototype because the compiler already knows what will happen

2 Likes

I didn’t know this was an “issue”.

Basically for static funcs I don’t prototype them. I keep em close, above the calling funcs.

This makes the reading of the code basically go bottom up. Everything else goes into headers, so it doesn’t matter. But I still kinda keep the order bottom up.

Usually it’s groups of funcs tho, with their helpers on top of them.

2 Likes

@admindev they go at the top because once main() runs everything gets put on the stack.

main() needs to be at the bottom of the stack so that the other functions will work as intended

2 Likes

not sure if joking :upside_down_face:

2 Likes

Wait… did I flip the order? xD

Sometimes I get it mixed up.

I best visualize it with plates.

2 Likes

:wink: Nah, I’m referring to programming styles in C. I believe the effects are the same.

1 Like

Ahh gotcha.

Something, something, something… LIFO

2 Likes

the declaration of functions has nothing to do with stack, once they are compiled they are put into .text section of the binary, the order doesn’t matter there. it only matters coz the compiler does single pass and goes top to bottom, so if the func wanna call another func, it needs to be declared first. So you either forward-declare it before it and write the full body below, or you just fully declare it on top.

4 Likes

Thanks, I guess I need to do more research.

Time to bust out my C book.

3 Likes

7 posts were merged into an existing topic: The Freeland

I use them only when I need to pass a callback I don’t want to define at the top.
Other than that, I use header files, though I don’t know enough about C langs to really posit

1 Like

Yes proto in one and only one .h file (one .h per .c/cc/cpp file) unless extern’ing. Never debated in professional coding that I’ve seen.

  1. The .h file becomes the genesis of the docs in many cases. The .h is your “man page” to consumers of your module.
  2. Forward declare and circular dependencies can be dealt with (linked lists, maps, containers of classes structs and enums may self refernece or circular reference.)
  3. Various attributes and defaults can be spec’d

I wasnt aware there was a serious debate about prototyping. Just do it. Relying on order is broken and does not scale.

I still put main last or in its own file though as its generally the least interesting part and/or a wrapper around an object(s) that can be called from within a .so or a standalone exe unaltered.

2 Likes

I assume we aren’t talking about redundant prototypes in-line with the caller of a function? That’s nuts and subject to profound bit-rot during devel (prototypes creeping away from the original definition).