Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there advantages of declaring functions before, after or inside main()?

I'm trying to learn C language for embedded systems. At the moment I'm learning the basics and couldn't find an answer to one of a fundamental question. When I wrote a simple C program I declared a function called maximum() in three ways. I will explain it by the following examples:

1-) Here in the below program the function is declared outside and before the main:

#include <stdio.h>

int maximum(int x, int y)
 {
    int z;
    z = (x >= y) ? x : y;
    return z;
}

int main(void)
{
    int result = maximum(30, 50);
    printf("%d", result);
    return(0);
}

2-) And now below the function is declared outside and after the main:

#include <stdio.h>    

int main(void)
{
    int result = maximum(30, 50);
    printf("%d", result);
    return(0);
}

int maximum(int x, int y)
 {
    int z;
    z = (x >= y) ? x : y;
    return z;
}

3-) And finally below the function is declared inside the main:

#include <stdio.h>

int main(void)
{
    int maximum(int x, int y)
 {
    int z;
    z = (x >= y) ? x : y;
    return z;
 }
    int result = maximum(30, 50);
    printf("%d", result);
    return(0);
}

I tried all those above and all executes without error. Is there any reason to prefer one to the others?

like image 653
floppy380 Avatar asked Dec 01 '22 13:12

floppy380


2 Answers

In standard C since C99, it is necessary to declare functions before you call them. This tells the compiler what type to expect of the return value, how many arguments it should pass, how it might need to convert them to correctly match the function's parameter types. That declaration does not need to be a definition of the function, however, and frequently it isn't.

Your (1) is fine in this regard, but your (2) is non-conforming. However, (2) happens to have return and parameter types that are consistent with older C expectations for calling an undeclared function, and many compilers will accept it and do the right thing with it under some circumstances.

Your (3) is non-conforming in a different way: C does not allow nested functions. You may put a function declaration inside another function, though there is little advantage to this, but not a whole function definition. One compiler family I know does accept that as an extension, and maybe others do, too, but under no circumstances should you rely on that.

Thus, of the three alternatives presented, (1) is the only one you should use. If you want to have more freedom to place the maximum() function then provide a forward declaration, with prototype, near the top of the file:

(4)

#include <stdio.h>    

// Forward declaration, including prototype:
int maximum(int x, int y);

int main(void)
{
    int result = maximum(30, 50);
    printf("%d", result);
    return(0);
}

int maximum(int x, int y)
 {
    int z;
    z = (x >= y) ? x : y;
    return z;
}
like image 179
John Bollinger Avatar answered Jan 13 '23 10:01

John Bollinger


Option 1 is valid and it works, but it tends to hide the main function at the end of your program.

Option 2 is not valid starting with C99 where there's no "default int" anymore.

Option 3 is not valid although many compilers support it.

What I would suggest is a mix of options 1 and 2. Declare the function prototype before main and define it later in the file:

#include <stdio.h>    

int maximum(int x, int y);

int main(void)
{
    int result = maximum(30, 50);
    printf("%d", result);
    return(0);
}

int maximum(int x, int y)
 {
    int z;
    z = (x >= y) ? x : y;
    return z;
}
like image 24
S.S. Anne Avatar answered Jan 13 '23 10:01

S.S. Anne