1
#include <stdio.h>

int add2nums( int, int);

void main(void)
{
  int y,a,b;

  printf("Enter 2 numbers\n");
  scanf("%d%d", &a, &b);

  y = add2nums(a,b);

  printf("a is %d\n", a);
  printf("b is %d\n", b);
  printf("y is %d\n", y);
}


int add2nums( int num1, int num2)
{
  int sum;
  sum = num1 + num2;
  return(sum);
}

So usually, when I create new functions in C the definition of the function is created before the main() function.

In my lecture, there is this example of how to create a function prototype and how they are created by declaring it before the main() function and then defining it after the end of the main() function. When running the program above, the following error comes up:

Line5: warning: return type of 'main' is not 'int' [-Wmain]|

What is happening? And why is the declaration of the function add2nums() occur twice once before main() and with no parameters?

int add2nums( int, int);

and then again after the end of main() with parameters num1 and num2

int add2nums( int num1, int num2)
Stack Danny
  • 7,754
  • 2
  • 26
  • 55

3 Answers3

2

There are two valid signatures for the main() function:

int main( void )
int main( int argc, char *argv[] )

Notice that both valid signatures have a return type of int. Any other return type, such as void is not valid and causes the compiler to output a warning message.

When the code calls a function, the compiler needs to know the signature of that called function. There are two ways to tell the compiler what the signature of the called function is:

  1. have the whole function listed before where it is called
  2. have a prototype (aka forward reference) of the function signature before where the function is called. In a prototype the compiler only needs the returned type and the types of the parameters. However, it is a good programming practice to have the names of the parameters listed in the prototype as a courtesy to the humans reading the code.
user3629249
  • 16,402
  • 1
  • 16
  • 17
  • The C standard specifies two forms of declaration for `main` that are standard. Those should generally be used by most programmers in most cases. But it does not make other cases invalid—C implementations may define other forms. They are merely not defined by the C standard, not invalid. – Eric Postpischil May 04 '19 at 16:57
  • Hi, thanks for your answers. So to solve this problem I changed to the following:void main() now is int main() and I placed a return(0) at the end of int main(). This worked with no problems. So I take it that the reason int main() function now returns a type int is because of calling the add2nums which returns an int or because it is saved in a variable of type int, namely y? which one is it? – Aragorn90th May 04 '19 at 17:02
  • @Aragorn90th : No the warning is entirely unrelated to calling `add2nums()`. You'd get that warning even with an empty `main()`. It is a warning rather than an error because ISO C90 at least _allowed_ implementation defined forms of main. But for strict portability it should be avoided, and in C++ is in any case prohibited. – Clifford May 04 '19 at 17:31
1
  1. In C language, the main function has a signature int main(), which lets you to return a value back to the OS, but you made the main() with no void return type. Because of that you've got the warning.
  2. int add2nums( int, int); is just a declaration of the function which lets the compiler make a reference to the function. That's because the compiler reads the file only once.
  3. Names of the parameters aren't required, because they aren't part of the signature.
Michel_T.
  • 2,741
  • 5
  • 21
  • 31
  • declaration is a way for the programmer to tell the compiler what kind of functions will be defined. a definition includes the body of the function (with source code). This is a C specific thing as the compiler reads your code from top to bottom once, instead of twice to figure what functions are in your source code. – Alexander Oh May 04 '19 at 15:46
  • The proper modern standard declarations of `main` are `int main(void)` and `int main(int argc, char *argv[])`, or equivalent, not `int main()`, although C implementations may allow it. – Eric Postpischil May 04 '19 at 16:54
1

Declaration of the function is the information for the compiler (not linker as another answer states) - what type the parameters of the function are and what type of the return value is.

So the names of the parameters are not needed.

0___________
  • 60,014
  • 4
  • 34
  • 74