Basically it means "nothing" or "no type"

There are 3 basic ways that void is used:

  1. Function argument: int myFunc(void) -- the function takes nothing.

  2. Function return value: void myFunc(int) -- the function returns nothing

  3. Generic data pointer: void* data -- 'data' is a pointer to data of unknown type, and cannot be dereferenced

Note: the void in a function argument is optional in C++, so int myFunc() is exactly the same as int myFunc(void), and it is left out completely in C#. It is always required for a return value.

Answer from Gerald on Stack Overflow
Top answer
1 of 3
4

If you don't have a forward declaration of your function before the place of usage, the compiler will create implicit declaration for you - with the signature int input(). It will take the name of the function you called, it will assume that the function is returning int, and it can accept any arguments (as Bartek noted in the comment).

For this function, the implicit declaration matches the real declaration, so you don't have problems. However, you should always be careful about this, and you should always prefer forward declarations instead of implicit ones (no matter if they are same or not). So, instead of just having forward declaration of the void prime() function (assuming that you will use it somewhere), you should also have a forward declaration of int input().

To see how can you pass any number of the arguments, consider this:

#include <stdio.h>

// Takes any number of the arguments
int foo();

// Doesn't takes any arguments
int bar(void)
{
    printf("Hello from bar()!\n");
    return 0;
}

int main()
{
    // Both works

    // However, this will print junk as you're not pushing
    // Any arguments on the stack - but the compiler will assume you are
    foo();

    // This will print 1, 2, 3
    foo(1, 2, 3);

    // Works
    bar();

    // Doesn't work
    // bar(1, 2, 3);

    return 0;
}

// Definition
int foo(int i, int j, int k)
{
    printf("%d %d %d\n", i, j, k);
    return 0;
}

So, inside the definition of the function you're describing function arguments. However, declaration of the function is telling the compiler not to do any checks on the parameters.

2 of 3
2

Not declaring a prototype and relying on default argument/return type promotion is dangerous and was a part of old C. In C99 and onward it is illegal to call a function without first providing a declaration or definition of the function.

my question is, is it necessary to declare a void function with not arguments?

Yes. For this you have to put void in the function parenthesis.

void foo(void);  

Declaring a function like

void foo();  

means that it can take any number of arguments.

🌐
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.
🌐
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().
Published   July 17, 2014
🌐
C-programming-simple-steps
c-programming-simple-steps.com › what-is-void.html
What is void in C - C Programming
Master C programming in simple steps! Newbie or advanced you can do this in simple steps! Here are the tutorials and examples that will show you how.
🌐
Florida State University
cs.fsu.edu › ~cop3014p › lectures › ch7 › index.html
Functions 2: Void (NonValue-Returning) Functions
//value-returning function call (assignment): y = 2.0 * sqrt(x); In contrast, a void function (method or procedure, in other languages) does not return a function value. Nor is it called from within an expression. Instead, the function call appears as a complete, stand-alone statement. One example is the get function associated with the istream and ifstream classes:
🌐
YouTube
youtube.com › watch
Void Functions | C Programming Tutorial - YouTube
An explanation of what void functions are in C and how to use them. Source code: https://github.com/portfoliocourses/c-example-code/blob/main/void_functions...
Published   July 17, 2024
🌐
E Computer Notes
ecomputernotes.com › home › pointer › void functions in c
Void Functions in C - Computer Notes
October 31, 2020 - A few illustrations of such functions are given below. 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.
Find elsewhere
🌐
Quora
quora.com › How-do-you-create-and-call-a-void-function-without-returning-anything-void-in-C-programming-language
How to create and call a void function without returning anything (void) in C programming language - Quora
Answer (1 of 3): The answer is simple, define a void function and return nothing.. void myfunchaha(){ printf(“I will return nothing hahaha”); // no need for return } That's it but if you want to know how this works on the background.
🌐
Learn C++
learncplusplus.org › home › c++ › what is a void function in the c programming language?
What is A Void Function In The C Programming language?
September 3, 2022 - At the end of void functions, you don’t need to (and shouldn’t) use the return command as there is no return value. Even without the return statement, control will return to the caller automatically at the end of the function. Yes, void functions may have parameters just like any regular function. For example, this analyze() function is a void function that has parameters.
🌐
YouTube
youtube.com › watch
C Programming Tutorial - Void Function - YouTube
In this #C #Programming #Tutorial I discuss the #void #function #type.To view this entire playlist:https://youtube.com/playlist?list=PLwTHcico4iPPdtKaloAXlbB...
Published   January 2, 2022
🌐
Educative
educative.io › answers › what-is-the-void-keyword-in-c
What is the void keyword in C?
foo(void) is used for a function that takes no arguments. Generic pointer declaration that has no type specified with it. Consider the code snippet below, which uses void as a pointer declaration.
🌐
University of Utah
users.cs.utah.edu › ~germain › PPS › Topics › C_Language › c_functions.html
Functions in the C programming Language
In C, the default is to pass by value. For example: // // C function using pass by value. (Notice no &) // void doit( int x ) { x = 5; } // // Test function for passing by value (i.e., making a copy) // int main() { int z = 27; doit( z ); printf("z is now %d\n", z); return 0; }
🌐
Yale University
cs.yale.edu › homes › aspnes › pinewiki › C(2f)Functions.html
C/Functions
The ints scattered about specify ... values in and out of the function and (usually for more complex types, since numerical types will often convert automatically) to detect type mismatches. If you want to define a function that doesn't return anything, declare its return type as void...
🌐
GNU
gnu.org › software › c-intro-and-ref › manual › html_node › Void-Pointers.html
Void Pointers (GNU C Language Manual)
{ int *p; /* Converts return value ... has a pointer type also converts. For example, supposing the function hack is declared to require type float * for its parameter, this call to hack will convert the argument to that type....
🌐
ThoughtCo
thoughtco.com › definition-of-void-958182
What Is the Definition of "Void" in C and C++?
April 28, 2019 - The void function accomplishes its task and then returns control to the caller. The void function call is a stand-alone statement. For example, a function that prints a message doesn't return a value.
🌐
Quora
quora.com › What-are-practical-examples-of-a-void-function
What are practical examples of a void function? - Quora
Answer (1 of 4): In C, perhaps you want a function that prints something out: [code]void PrintHelloWorld () { printf ( "Hello World.\n") ; } int main (void) { PrintHelloWorld () ; } [/code]A void function is simply a function that doesn’t need to return anything. It can either be like the...
🌐
Crasseux
crasseux.com › books › ctutorial › void.html
void - The GNU C Programming Tutorial - at Crasseux (?)
Finally, a function call can be cast to void in order to explicitly discard a return value. For example, printf returns a value, but it is seldom used.
🌐
YouTube
youtube.com › watch
Return Statement In A Void Function | C Programming Tutorial - YouTube
How to use a return statement in a void function in C. Source code: https://github.com/portfoliocourses/c-example-code/blob/main/void_function_return.c. Ch...
Published   October 23, 2022