1

I have seen in C++ program, during function declaration if there is no parameter for the function void is declared as parameter like this:

int F1(void)

How is it different than:

int F1()
Denis
  • 1,219
  • 1
  • 10
  • 15

3 Answers3

2

There is no difference. Using void is just a more explicit way to declare the same thing. Personally, I never use that syntax and rarely see anyone else use it either.

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
1

It's the same thing in C++, and is a holdover from C.

Here's an excerpt from the C++ 2003 standard (C.1.6):

Change: In C++, a function declared with an empty parameter list takes no arguments. In C, an empty parameter list means that the number and type of the function arguments are unknown"
Example:

 int f(); // means int f(void) in C++
          // intf(unknown) in C

Rationale: This is to avoid erroneous function calls (i.e. function calls with the wrong number or type of arguments).

Effect on original feature: Change to semantics of well-defined feature. This feature was marked as “obsolescent” in C.

Buddy
  • 10,874
  • 5
  • 41
  • 58
0

Both of them are exactly the same, leaving the argument field empty as () is the one I prefer, some prefer writing (void) just so that someone editing the code may be ensured that no arguments are required. Makes no difference though, just a readability thing.