void foo(void);

That is the correct way to say "no parameters" in C, and it also works in C++.

But:

void foo();

Means different things in C and C++! In C it means "could take any number of parameters of unknown types", and in C++ it means the same as foo(void).

Variable argument list functions are inherently un-typesafe and should be avoided where possible.

Answer from Daniel Earwicker on Stack Overflow
Top answer
1 of 6
339
void foo(void);

That is the correct way to say "no parameters" in C, and it also works in C++.

But:

void foo();

Means different things in C and C++! In C it means "could take any number of parameters of unknown types", and in C++ it means the same as foo(void).

Variable argument list functions are inherently un-typesafe and should be avoided where possible.

2 of 6
105

There are two ways for specifying parameters in C. One is using an identifier list, and the other is using a parameter type list. The identifier list can be omitted, but the type list can not. So, to say that one function takes no arguments in a function definition you do this with an (omitted) identifier list

void f() {
    /* do something ... */
}

And this with a parameter type list:

void f(void) {
    /* do something ... */
}

If in a parameter type list the only one parameter type is void (it must have no name then), then that means the function takes no arguments. But those two ways of defining a function have a difference regarding what they declare.

Identifier lists

The first defines that the function takes a specific number of arguments, but neither the count is communicated nor the types of what is needed - as with all function declarations that use identifier lists. So the caller has to know the types and the count precisely before-hand. So if the caller calls the function giving it some argument, the behavior is undefined. The stack could become corrupted for example, because the called function expects a different layout when it gains control.

Using identifier lists in function parameters is deprecated. It was used in old days and is still present in lots of production code. They can cause severe danger because of those argument promotions (if the promoted argument type do not match the parameter type of the function definition, behavior is undefined either!) and are much less safe, of course. So always use the void thingy for functions without parameters, in both only-declarations and definitions of functions.

Parameter type list

The second one defines that the function takes zero arguments and also communicates that - like with all cases where the function is declared using a parameter type list, which is called a prototype. If the caller calls the function and gives it some argument, that is an error and the compiler spits out an appropriate error.

The second way of declaring a function has plenty of benefits. One of course is that amount and types of parameters are checked. Another difference is that because the compiler knows the parameter types, it can apply implicit conversions of the arguments to the type of the parameters. If no parameter type list is present, that can't be done, and arguments are converted to promoted types (that is called the default argument promotion). char will become int, for example, while float will become double.

Composite type for functions

By the way, if a file contains both an omitted identifier list and a parameter type list, the parameter type list "wins". The type of the function at the end contains a prototype:

void f();
void f(int a) {
    printf("%d", a);
}

// f has now a prototype. 

That is because both declarations do not say anything contradictory. The second, however, had something to say in addition. Which is that one argument is accepted. The same can be done in reverse

void f(a) 
  int a;
{ 
    printf("%d", a);
}

void f(int);

The first defines a function using an identifier list, while the second then provides a prototype for it, using a declaration containing a parameter type list.

Top answer
1 of 2
38

C and C++ are different in this respect.

C 2011 Online Standard

6.7.6.3 Function declarators (including prototypes)
...
10 The special case of an unnamed parameter of type void as the only item in the list specifies that the function has no parameters.
...
14 An identifier list declares only the identifiers of the parameters of the function. An empty list in a function declarator that is part of a definition of that function specifies that the function has no parameters. The empty list in a function declarator that is not part of a definition of that function specifies that no information about the number or types of the parameters is supplied.145)

In short, an empty parameter list in a function declaration indicates that the function takes an unspecified number of parameters, while an empty parameter list in a function definition indicates that the function takes no parameters.

T foo( void ); // declaration, foo takes no parameters
T bar();       // declaration, bar takes an *unspecified* number of parameters

T foo( void ) { ... } // definition, foo takes no parameters
T bar() { ... }       // definition, bar takes no parameters

As far as C is concerned, you should never use an empty identifier list in a function declaration or definition. If a function is not meant to take any parameters, specify that by using void in the parameter list.

Online C++ standard

8.3.5 Functions [dcl.fct]
...
4 The parameter-declaration-clause determines the arguments that can be specified, and their processing, when the function is called. [ Note: the parameter-declaration-clause is used to convert the arguments specified on the function call; see 5.2.2. β€” end note ] If the parameter-declaration-clause is empty, the function takes no arguments. A parameter list consisting of a single unnamed parameter of non-dependent type void is equivalent to an empty parameter list. Except for this special case, a parameter shall not have type cv void. If the parameter-declaration-clause terminates with an ellipsis or a function parameter pack (14.5.3), the number of arguments shall be equal to or greater than the number of parameters that do not have a default argument and are not function parameter packs. Where syntactically correct and where β€œ...” is not part of an abstract-declarator, β€œ, ...” is synonymous with β€œ...”. [Example: the declaration
    int printf(const char*, ...);
declares a function that can be called with varying numbers and types of arguments.
    printf("hello world");
    printf("a=%d b=%d", a, b);
However, the first argument must be of a type that can be converted to a const char* β€” end example ] [ Note: The standard header <cstdarg> contains a mechanism for accessing arguments passed using the ellipsis (see 5.2.2 and 18.10). β€” end note ]

In the case of C++, an empty parameter list in either a declaration or a definition indicates that the function takes no arguments, and is equivalent to using a parameter list of void.

2 of 2
45

In C, a function with an empty parameter list () can take anything for its arguments. Literally anything. This is usually used to implement a function which can take a variable number of arguments, though these days it's considered preferable to use the more explicit ellipsis syntax (...) for these functions.

In C, a function with the parameter list (void) explicitly takes nothing for its arguments. That means the compiler can actually tell you you've made a mistake if you try to pass something.

In C++, these function declarations are equivalent. A blank parameter list means "no parameters" the same as void does.

🌐
Florida State University
cs.fsu.edu β€Ί ~cop3014p β€Ί lectures β€Ί ch7 β€Ί index.html
Functions 2: Void (NonValue-Returning) Functions
/* This program demos void functions using value parameters vs. reference parameters Reference Parameter: If a formal parameter is a reference parameter, it receives the address of the corresponding actual parameter A reference parameter stores the address of the corresponding actual parameter During program execution to manipulate the data, the address stored in the reference parameter directs it to the memory space of the corresponding actual parameter */ //header files #include<iostream> using std::cout; using std::cin; using std::endl; //prototype functions void readDate(int& month, int& d
🌐
SEI CERT
wiki.sei.cmu.edu β€Ί confluence β€Ί display β€Ί c β€Ί DCL20-C.+Explicitly+specify+void+when+a+function+accepts+no+arguments
DCL20-C. Explicitly specify void when a function accepts no arguments - SEI CERT C Coding Standard - Confluence
Defining a function with a void argument list differs from declaring it with no arguments because, in the latter case, the compiler will not check whether the function is called with parameters at all [TIGCC, void usage]. Consequently, function calling with arbitrary parameters will be accepted without a warning at compile time.
🌐
Yale University
cs.yale.edu β€Ί homes β€Ί aspnes β€Ί pinewiki β€Ί C(2f)Functions.html
C/Functions
1 /* Prints "hi" to stdout */ 2 void 3 helloWorld(void) 4 { 5 puts("hi"); 6 } It is not strictly speaking an error to omit the second void here. Putting void in for the parameters tells the compiler to enforce that no arguments are passed in. If we had instead declared helloWorld as Β· 1 /* ...
🌐
Reddit
reddit.com β€Ί r/c_programming β€Ί newbie question - what is void as a function parameter?
Newbie Question - What is Void as a Function Parameter? : r/C_Programming
December 3, 2020 - Even in modern C, a function declaration with an empty parameter list does not mean the function takes no parameters, it means whatever parameters it does take, if any, aren't specified by the declaration. So that means the standards committee needed some other way to say "this function takes no parameters". And that's how that weird void ...
🌐
Jim Fisher
jameshfisher.com β€Ί 2016 β€Ί 11 β€Ί 27 β€Ί c-void-params
What does `void` mean as a function parameter in C?
`void` in a C function parameter list means the function takes no arguments, whereas an empty list allows for an unspecified number of arguments.
🌐
University of Utah
users.cs.utah.edu β€Ί ~germain β€Ί PPS β€Ί Topics β€Ί C_Language β€Ί c_functions.html
C Programming - Functions
// void doit( int & x ) { x = 5; } // // Test code for passing by a variable by reference // int main() { int z = 27; doit( z ); printf("z is now %d\n", z); return 0; } With standard C you have to put the & in the calling location as opposed to next to the parameter in the function declaration; ...
Find elsewhere
🌐
IncludeHelp
includehelp.com β€Ί c β€Ί void-pointer-as-function-argument.aspx
void pointer as function argument in C programming
#include <stdio.h> //function prototype void printString(void *ptr); int main() { char *str="Hi, there!"; printString(str); return 0; } //function definition void printString(void *ptr) { printf("str: %s\n",ptr); } ... In this example the function parameter ptr is a void pointer and character pointer (string) str will be assigned in it and program will print the string through void pointer.
🌐
E Computer Notes
ecomputernotes.com β€Ί home β€Ί pointer β€Ί void functions in c
Void Functions in C - Computer Notes
October 31, 2020 - void Write (void) { printf("You need a compiler for learning C language.\n"); } The first line in the above definition may also be written as Β· void Write () Program presents an example where a void function is defined to display a message. Illustrates a void function with void parameter list.
🌐
Mtsu
cs.mtsu.edu β€Ί ~cs1170 β€Ί manual β€Ί lab8 β€Ί lab8.html
Lab 8 -- C++ void Functions, Value and Reference Parameters, Local Variables
Function Activation To activate a void function with value parameters, we specify the name of the function and provide the actual arguments enclosed in parentheses. The order and types of the list of arguments should correspond exactly to those of the formal parameters declared in the function ...
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί c language β€Ί c-function-argument-return-values
C Function Arguments and Function Return Values - GeeksforGeeks
January 10, 2025 - Function declaration : void function ( int ); Function call : function( x ); Function definition: void function( int x ) { statements; } ... // C code for function // with argument but no return value #include <stdio.h> void function(int, int[], ...
🌐
ThoughtCo
thoughtco.com β€Ί definition-of-void-958182
What Is the Definition of "Void" in C and C++?
April 28, 2019 - When void appears in a pointer declaration, it specifies that the pointer is universal. When used in a function's parameter list, void indicates that the function takes no parameters.
🌐
Reddit
reddit.com β€Ί r/cpp_questions β€Ί help with parameters, void function, calling functions, and returns
r/cpp_questions on Reddit: Help with parameters, void function, calling functions, and returns
January 19, 2023 -

Hello,

I am a beginner at coding and I am currently learning C++ as my first language. I started 3 days ago, and I am using Codecademy to learn. Right now I am doing a subsection "Parameters and Arguments" in the section "Functions." The way I was taught to use the return function is as follows in the code that they gave me:

#include <iostream>

bool morning = true;

std::string make_sandwich(){
   
    std::string sandwich = "";
    sandwich += "bread\n";
    if (morning == true) {
        sandwich += "egg\n;
    }
    sandwich += "cheese\n";
    sandwich += "bread\n";
    return sandwich;
    
    }

    int main {
       
        std::cout << "Your sandwich now:\n" << make_sandwich();
    }

This was the previous lesson before I was given an assignment, and I have been trying for a long time to do it. It told me: "Let's build a function that prints out the current emergency number, whatever it is from now on. Above `main()`, define a `void` function `get_emergency_number()` that accepts one parameter: a string with the name `emergency_number`."

There are some other steps, but I cannot proceed until I have finished this one.

The code (without // comments [it is kind of hard to understand the comments because it is also for other steps]) is as follows:

#include <iostream>

int main() {

    int emergency_number;

        std::string old_emergency_number = "999";

        std::string new_emergency_number = "0118 999 991 999 725 3";

}

This is my most recent try:

#include <iostream>

void get_emergency_number() {
    std::string int emergency_number;
}

int main() {

    int emergency_number;

        std::string old_emergency_number = "999";

        std::string new_emergency_number = "0118 999 991 999 725 3";

}

I had many, many more tries, but I could not get this to work, I even had a somewhat-experienced coder look at the code, but they could not figure it out.

Please help me and do not just fix, I would prefer an explanation that will serve good use to me for my knowledge.

Thank you,

Zach

Top answer
1 of 5
6
define a void function get_emergency_number() that accepts one parameter: a string with the name emergency_number." void get_emergency_number(std::string emergency_number) { } A function with get in its name that doesn't return anything is confusing. Have you considered : https://www.learncpp.com/cpp-tutorial/introduction-to-function-parameters-and-arguments/
2 of 5
3
Hint: what is the difference between these *signatures* (free vocab lesson for the day)? void print_number(); void print_number(std::string); void print_number(const std::string&); The first signature takes no arguments. So it does not do what you want. The other signatures take one argument, and they are much closer to what I would expect you want. Why? This version is a very reasonable first attempt: void print_number(std::string); It takes a single std::string argument by value. There are potential performance implications for your program because the compiler is required to give that function call its own unique instance of its argument, which potentially involves making a copy. This version void print_number(const std::string&); is better for a few reasons: It passes by reference which means that the original string object is shared It also tells the compiler "inside the scope of this function, treat this variable as constant and make any attempt to modify it an error". Today you probably don't care too much about this, but long term "const correctness" is very powerful tool for avoiding entire categories of bugs. So let's start with #2. Your code skeleton should look something like this now: void print_number(std::string number) { // Your implementation goes here } std::string get_number() { // Your implementation goes here return "1 2 3 4 5"; } int main(int, const char**) { const std::string new_number = get_number(); print_number(new_number); return 0; } Hope this helps.
🌐
Reddit
reddit.com β€Ί r/c_programming β€Ί void()
r/C_Programming on Reddit: void()
June 28, 2023 -

I am trying to learn C for the first time.

Can someone please explain to me when it is necessary to use the void function? I don't understand what is meant by it doesn't return a value.

Top answer
1 of 3
13
A return value is the data that the function evaluates to when it completes. If you have a function like this: int multiply(int x, int y) { return x * y; } Then you can write something like this: int result = multiply(6, 7); multiply will run, and the return value will be an int with the value of 42, which will then be assigned to the variable result. When you declare and define a function, you need to specify the return type, because the compiler needs to know how much space to use to store the return value, as well as making sure that you're not assigning the value to a mismatched type, or using it where a function expects a different type. In the case of a void function, you're telling the compiler that your function is not going to return any value at all. void say_hello(char *name) { printf("Hello, %s!\n", name); } In this case you just run the function, and it prints the message and then ends. There's no return value that the function gives you that you can use elsewhere in your program. ie. this would work: say_hello("George"); but this would not: int result = say_hello("George");
2 of 3
12
Void itself is not a function. It is used to define void functions, which are functions that don't return a value. You use it whenever you want to write a function that does not return a value. In some other languages, void functions are called procedures. For example, if you write a logging function, its purpose is to output data to a file. It doesn't calculate or read any information, so it doesn't need to return anything to the caller. The same keyword, void, is also used to define that a function does not take any parameters. So void hello(void) { /* ... */ } is a function that does not take any input parameters, and does not return any value. Finally, there are void pointers, but I think that's for another time.
🌐
Quora
quora.com β€Ί What-can-void-data-be-in-the-C-language-if-it-is-an-argument-to-a-function
What can void data be in the C language if it is an argument to a function? - Quora
Answer (1 of 2): [code]int func(void); [/code]If you declare any function with a void parameter, it is the standard way to define that the function will not take any parameters. This is explained in the C99 standard: > The special case of an ...
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί c language β€Ί void-pointer-c-cpp
void Pointer in C - GeeksforGeeks
void pointers in C are used to implement generic functions in C. For example, compare function which is used in qsort(). void pointers used along with Function pointers of type void (*)(void) point to the functions that take any arguments and ...
Published Β  July 17, 2014