0
int func (int,int);

int main ()
{ 
     printf("hello");
}

Consider the above function. What is the purpose of defining functions like func? I have seen this repeatedly.

jack.math
  • 29
  • 7
  • 1
    That's not a function definition, but a **function prototype**. Prototypes reports the minimal info's to allow compiler to use the function that can be defined elsewhere (i.e. in an object library). Knowing from prototype what a function returns and which parameters gets is enough to correctly link it to your code. – Frankie_C Dec 10 '17 at 16:27
  • 3
    Possible duplicate of [What are "prototypes" in a C program?](https://stackoverflow.com/questions/23661729/what-are-prototypes-in-a-c-program) – Raymond Chen Dec 10 '17 at 16:31

4 Answers4

1
int func(int, int);

does not define a function, but declares it by providing its prototype. It's the "promise" to the compiler, that latest the linker will provide a module exposing such a function.

The prototype for a function covers its:

  • return type
  • name
  • the type and order of the arguments

The arguments' names are not relevant in the context of a declaration.

The function's arguments' names only need to be provided for the function's implementation, which might look like this:

int func(int a, int b)
{
  return a + b;
}

On the other hand this

int func(int a, int)
{
  return a;
}

is not valid.

The C Standard clearly states:

6.9.1/5 Function definitions

If the declarator includes a parameter type list, the declaration of each parameter shall include an identifier, except for the special case of a parameter list consisting of a single parameter of type void, in which case there shall not be an identifier. No declaration list shall follow.

alk
  • 69,737
  • 10
  • 105
  • 255
  • 2
    Have you tried to compile this "not valid" code? it looks valid. Not providing the name of the parameter in the implementation is a technique used to suppress the "unused parameter" warning – RSFalcon7 Dec 10 '17 at 16:40
  • @RSFalcon7: I'd expect trying to compile `int func(int a, int) { return a; }` to result in an error like this: `error: parameter name omitted` in C at least, unsure about C++. – alk Dec 10 '17 at 16:43
  • @RSFalcon7: "*used to suppress the "unused parameter" warning*" I'd consider this a vendor specific extension to the C Standard. Please prove me wrong by citing the relevant parts of C Standard. – alk Dec 10 '17 at 17:01
  • @RSFalcon7: Please see this discussion: https://stackoverflow.com/q/8776810/694576 There seems to be one more slight difference between C and C++. – alk Dec 10 '17 at 17:05
1

All the other answers are not answering the correct question explicitly, so I'm adding one here.

First of all, it's not a function definition because the body is missing. This semantic is called function declaration.

Before calling a function, the compiler needs to know what exactly the function is. This is named a "prototype". A prototype must be known before generating correct code to call a function. Consider this code:

// No previous info
int a = func(1, 3);

The compiler doesn't know if it is calling int func(int, int) or long func(char, double), nor can it perform error checking if the actual function is FILE* func(void*).

With a correct prototype privided, the compiler is able to perform necessary checking and generate the corresponding code of that function call.

iBug
  • 35,554
  • 7
  • 89
  • 134
0

It is not function defination but it is a functions declaration. A function declaration informs the compiler of the number of parameters the function has, the type of the function parameters and the type of the function return value.

1) It tells the return type of the data that the function will return.

2) It tells the number of arguments passed to the function.

3) It tells the data types of the each of the passed arguments.

4) Also it tells the order in which the arguments are passed to the function.

5) It tells the name (identifier) of the function.

msc
  • 33,420
  • 29
  • 119
  • 214
0

This is just a function declaration of a function to tell compiler that a function with name func exists having definition at end.
Consider this example:

   #include<stdio.h>
    int func (int,int);
    int main()
    {
         printf("hello\n");
         int c= func(5,4);
         printf("%d\n",c);

    }
    int func(int a, int b)
    {

         return a+b;
    }
Lalit Verma
  • 782
  • 10
  • 25
  • `return` isn't a function. There is no need to warp its argument into parenthesis. – alk Dec 10 '17 at 16:32
  • 2
    Formally it's not wrong, not wrong as `return ((a) + (b));` would not be wrong. – alk Dec 10 '17 at 16:35