Function declaration vs. definition C

Kristián Stroka

I have a simple code, where my functions are declared before main function like that:

int function1();
int function2();

int main() {
   /* ... */
   function1(x,y);
   function2(x,y);
   /* .... */
}

int function1(int x, float y) { /* ... */ }
int function2(int x, float y) { /* ... */ }

And after my main function I have definitions of functions:

Is there some difference, when I declare functions before main like this?

int function1(int x, float y);
int function2(int x, float y);

int main() {
   /* ... */
   function1(x,y);
   function2(x,y);
   /* .... */
}

int function1(int x, float y) { /* ... */ }
int function2(int x, float y) { /* ... */ }
Brian McFarland

Yes, they are different.

In the first example, you are just telling the compiler about the name and return type of the function and nothing of its expected arguments.

In the second example you are telling the compiler the full signature of the functions, both return type and expected arguments, prior to calling them.

The second form is pretty much universally better as it helps you compiler do a better job warning you when you have the wrong type or number of arguments when calling the function.

Also note int function() in C is a function that can accept any arguments, not a function that accepts no arguments. For that you need an explicit void, i.e int function(void). This mostly trips up those coming to C from C++.

See also: Why does a function with no parameters (compared to the actual function definition) compile?

To demonstrate why the first, antiquated form is bad in modern C, the following program compiles without warning with gcc -Wall -ansi -pedantic or gcc -Wall -std=c11.

#include<stdio.h>
int foo();

int main(int argc, char**argv)
{
  printf("%d\n", foo(100));
  printf("%d\n", foo(100,"bar"));
  printf("%d\n", foo(100,'a', NULL));
  return 0;
}

int foo(int x, int y)
{
  return 10;
}

UPDATE: M&M brought to my attention that my example uses int not float for the functions. I think we can all agree that declaring int function1() is bad form, but my statement that this declaration accepts any arguments is not quite correct. See Vlad's answer for relevant spec section explaining why that is the case.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Declaration vs definition in C

From Dev

C function definition and declaration

From Dev

Weird declaration and definition of a function in C

From Dev

VS2015 C++ : unable to match function definition to an existing declaration

From Dev

VS2015 C++ : unable to match function definition to an existing declaration

From Dev

C using a forward declaration within a function definition

From Dev

C vs C++ switch statement variable definition vs declaration

From Dev

Variable declaration vs definition

From Dev

function declaration without definition

From Dev

Inconsistent declaration and definition in C

From Dev

Inconsistent declaration and definition in C

From Dev

array definition vs array declaration

From Dev

c++: unable to match function definition to an existing declaration

From Dev

C++ template : unable to match function definition to an existing declaration

From Dev

C++: location of definition/declaration of non-member function

From Dev

Separation of function declaration and definition in Swift

From Dev

Function definition precedes declaration in namespace

From Dev

C++ double declaration / definition?

From Dev

C++ Namespace Declaration and Definition

From Dev

C++: Why does function declaration is allowed inside another function but not function definition?

From Dev

Purpose of function declaration within function definition

From Dev

Function declaration within a function definition with wrong signature?

From Dev

Memory allocation in Java - Declaration vs Definition

From Dev

Prototype vs function definition

From Dev

Prototype vs function definition

From Dev

C++11: Explicit instantiation declaration vs. explicit instantiation definition

From Dev

Function definition doesn't match its declaration in C but does in C++

From Dev

Extern function pointer declaration and type inferred definition

From Dev

No GCC warning for function with declaration different from definition

Related Related

  1. 1

    Declaration vs definition in C

  2. 2

    C function definition and declaration

  3. 3

    Weird declaration and definition of a function in C

  4. 4

    VS2015 C++ : unable to match function definition to an existing declaration

  5. 5

    VS2015 C++ : unable to match function definition to an existing declaration

  6. 6

    C using a forward declaration within a function definition

  7. 7

    C vs C++ switch statement variable definition vs declaration

  8. 8

    Variable declaration vs definition

  9. 9

    function declaration without definition

  10. 10

    Inconsistent declaration and definition in C

  11. 11

    Inconsistent declaration and definition in C

  12. 12

    array definition vs array declaration

  13. 13

    c++: unable to match function definition to an existing declaration

  14. 14

    C++ template : unable to match function definition to an existing declaration

  15. 15

    C++: location of definition/declaration of non-member function

  16. 16

    Separation of function declaration and definition in Swift

  17. 17

    Function definition precedes declaration in namespace

  18. 18

    C++ double declaration / definition?

  19. 19

    C++ Namespace Declaration and Definition

  20. 20

    C++: Why does function declaration is allowed inside another function but not function definition?

  21. 21

    Purpose of function declaration within function definition

  22. 22

    Function declaration within a function definition with wrong signature?

  23. 23

    Memory allocation in Java - Declaration vs Definition

  24. 24

    Prototype vs function definition

  25. 25

    Prototype vs function definition

  26. 26

    C++11: Explicit instantiation declaration vs. explicit instantiation definition

  27. 27

    Function definition doesn't match its declaration in C but does in C++

  28. 28

    Extern function pointer declaration and type inferred definition

  29. 29

    No GCC warning for function with declaration different from definition

HotTag

Archive