3

I have a function, can write in 2 ways.

void function(void) {
        // operations....
}

and

void function() {
       // operations......
}

Both functions are of same prototype. Why we have to mention void as argument at function definition?

JEM
  • 53
  • 1
  • 5

2 Answers2

10

No, both have different prototypes.

Compile the below programs you will understand.

void function1(void)
{
   printf("In function1\n");
}

void function2()
{
   printf("In function2\n");
}

int main()
{
   function1();
   function2(100); //Won't produce any error
   return 0;
}  

Program 2:

 #include <stdio.h>
 void function1(void)
 {
    printf("In function1\n");
 }

 void function2()
 {
    printf("In function2\n");
 }

int main()
{
    function1(100);   //produces an error
    function2();
    return 0;
}
gangadhars
  • 2,584
  • 7
  • 41
  • 68
3

The correct way to say "no parameters" in C and C++ is

void function(void);

But when we write

void function();

It means a little different way in C and C++! It means "could take any number of parameters of unknown types", and in C++ it means the same as function(void).