0

I have seen some programmers code looking as below

Where after they have declared a struct they have two similar function pointing to the struct

What is the porpouse of the first void Point_print(const struct screen* self); when below of the main function there is another void Point_print(const struct screen* self); doing all the expressions

struct screen{
    double x;
    double y;
};

void Point_print(const struct screen* self); // <-- what is the purpose of having this function?



int main(int argc, const char * argv[]) {
    
    struct screen aScreen;
    aScreen.x = 1.0;
    aScreen.y = 2.0;
    
    Point_print(&aScreen);
    return 0;
}

void Point_print(const struct screen * self){ // <-- this function is doing the work?
    printf("x:%f, y:%f",(*self).x,(*self).y);
}


Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 1
    Code is read from top to bottom. For main() to know how Point_print looks, it needs a declaration (the first one), what the function does can be deferred to the function definition (the last one). – RetardedHorse Jan 19 '22 at 12:19
  • ok ? , but the use of two Point_print() shouldn't it be enough with the second one? just moving the second the Point_print() in top of main() and having just one? –  Jan 19 '22 at 12:23
  • 1
    You can move the second one to the top as well yes, and remove the first one if you do. Normally the first one is in a header file (*.h) and the second one in the source file (*.c) – RetardedHorse Jan 19 '22 at 12:24
  • 1
    It's a forward declaration, so the compiler knows about a function `Point_print` within `main` even if its defined somewhere else/later – Odysseus Jan 19 '22 at 12:24
  • Functions must be defined or declared before they are called in modern C (meaning C99 or later). The declaration before `main()` allows `main()` to call the function. You could move the definition before `main()` instead. – Jonathan Leffler Jan 19 '22 at 12:29
  • @RetardedHorse but even in the header file i have seen booth function being declare first function just empty and then the same function who do all the work. And some of them copy the both functions and paste it on the main file so there is now 4 of them –  Jan 19 '22 at 12:31
  • also why do they put the second function under main ? isn't the code been read from top to bottom? how can it find the "function who prints it out" when it isn't being declare on top?? and how come that having to functions with the same name dosen't crash(Redefinition of 'Point_print')having the same name? –  Jan 19 '22 at 12:38
  • 2
    @dinolin a function _definition_ in header file us usually the wrong way – Jabberwocky Jan 19 '22 at 12:39
  • @dinolin This sounds like terrible practice and a misunderstanding of how to properly include/link. I generally would avoid forward declarations if its not explicitly required – Odysseus Jan 19 '22 at 12:41

2 Answers2

2
// this is called function signature or declaration, 
// and typically it's what's found in
// header files, this is all that's needed to properly generate the code
// to call this function.

void Point_print(const struct screen* self); // <-- what is the purpose of having this function?

Now that it's declared you can use it:

int main(int argc, const char * argv[]) {
    
    struct screen aScreen;
    aScreen.x = 1.0;
    aScreen.y = 2.0;
    
    Point_print(&aScreen); // <--- it's the use here
    return 0;
}

This is the function body or implementation:

void Point_print(const struct screen * self){ // <-- this function is doing the work?
    printf("x:%f, y:%f",(*self).x,(*self).y);
}

When the linking step occurs it resolves the "link" between the "calling points" and where the actual body of the function.

One way to experience is to comment out the function body part completely and try compiling the file with main in it, it will compile correctly, however will spew out linking error.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
Ahmed Masud
  • 21,655
  • 3
  • 33
  • 58
  • ok? why to they put the second function under main ? isn't the code been read from to bottom? and how come that having to functions with the same name dosen't crash`(Redefinition of 'Point_print')`having the same name? –  Jan 19 '22 at 12:36
  • @dinolin [This](https://stackoverflow.com/a/8827247/898348) should answer your question. – Jabberwocky Jan 19 '22 at 12:37
  • 2
    @dinolin the line on the top is just a declaration but no definition of the function, therefore it is no redefinition. If it would be defined empty on the top, it would indeed be an error: `void Point_print(const struct screen *self) { }` – Odysseus Jan 19 '22 at 12:52
0

Compilers read code from top to bottom. Hence if there was no declaration of Point_print above main, the compiler would not know what arguments and return values to expect from Point_print by the time it reads the first call to it.

Here's a more detailed article about function declarations:

https://www.programiz.com/c-programming/c-user-defined-functions

wquinoa
  • 122
  • 8