๐ŸŒ
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.
๐ŸŒ
ThoughtCo
thoughtco.com โ€บ definition-of-void-958182
What Is the Definition of "Void" in C and C++?
April 28, 2019 - When used in a function's parameter list, void indicates that the function takes no parameters. Void functions, also called nonvalue-returning functions, are used just like value-returning functions except void return types do not return a value ...
๐ŸŒ
Florida State University
cs.fsu.edu โ€บ ~cop3014p โ€บ lectures โ€บ ch7 โ€บ index.html
Functions 2: Void (NonValue-Returning) Functions
Void functions are created and used just like value-returning functions except they do not return a value after the function executes. In lieu of a data type, void functions use the keyword "void." A void function performs a task, and then control returns back to the caller--but, it does

in programming languages, a keyword indicating the absence of data

The void type, in several programming languages, more so curly bracket programming languages derived from C and ALGOL 68, is the return type of a function that returns normally, but provides no โ€ฆ Wikipedia
๐ŸŒ
Wikipedia
en.wikipedia.org โ€บ wiki โ€บ Void_type
Void type - Wikipedia
2 weeks ago - The void type, in several programming languages, more so curly bracket programming languages derived from C and ALGOL 68, is the return type of a function that returns normally, but provides no result value to its caller. Usually such functions are called for their side effects, such as performing ...
๐ŸŒ
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.
Published ย  July 17, 2014
๐ŸŒ
Reddit
reddit.com โ€บ r/explainlikeimfive โ€บ eli5: what is the purpose of void in c?
r/explainlikeimfive on Reddit: ELI5: What is the purpose of void in C?
March 8, 2024 -

I just began studying C and I cannot, for the life of me, understand void.

I have read and listened to many people say "It does not return a value". Ok? What is a value? Why wouldn't we want to return it? What is the difference between "void main" and "int main"? Can we just use int for everything and ignore void altogether?

In what situations is void used? In what situations is void better? etc. Please help I don't get it at all!

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.

๐ŸŒ
Yale University
cs.yale.edu โ€บ homes โ€บ aspnes โ€บ pinewiki โ€บ C(2f)Functions.html
C/Functions
The ints scattered about specify ... 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....
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. Let me explain Guillermo's answer a bit more deeper. Sinc...
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ what-is-the-void-keyword-in-c
What is the void keyword in C?
The literal meaning of void is empty or blank. In C, void can be used as a data type that represents no data. ... Return type of a function that returns nothing.
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.

๐ŸŒ
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 - In C and C++ programing, the void term means โ€œno value is returnedโ€. In math, a function returns a value, i.e. y = f (x); Here f(x) is a function that works with variable x and y is the output of this function.
๐ŸŒ
Quora
quora.com โ€บ Why-do-we-use-a-void-function-in-the-C-language
Why do we use a void function in the C language? - Quora
Answer (1 of 4): Void function are simply those functions which will not return any value. For which purpose a function is created determines either it will be a returning function or a void function.
๐ŸŒ
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.
๐ŸŒ
Uregina
labs.cs.uregina.ca โ€บ 110 โ€บ functions โ€บ void-function.html
Void Functions
If a function is a value-returning function, the returnDataType will be something like int, float, or char and so on. If the function does not return any value, we use the word void for the return data type. Once a function is declared, it must be defined somewhere in the program.
๐ŸŒ
Crasseux
crasseux.com โ€บ books โ€บ ctutorial โ€บ void.html
void - The GNU C Programming Tutorial - at Crasseux (?)
The void data type was introduced to make C syntactically consistent. The main reason for void is to declare functions that have no return value.
๐ŸŒ
E Computer Notes
ecomputernotes.com โ€บ home โ€บ pointer โ€บ void functions in c
Void Functions in C - Computer Notes
October 31, 2020 - Functions may be return type functions and non-return type functions. The non-return type functions do not return any value to the calling function; the type of such functions is void. These functions may or may not have any argument to act upon.
๐ŸŒ
Sololearn
sololearn.com โ€บ en โ€บ Discuss โ€บ 2756726 โ€บ what-is-the-use-of-void-before-the-function
what is the use of void before the function? | Sololearn: Learn to code for FREE!
Overall, `void` is an important keyword in C and C++ programming languages, as it is used to indicate functions that do not return any values or take any arguments.
๐ŸŒ
Quora
quora.com โ€บ Why-do-we-use-void-in-C-programming-functions-instead-of-format-specifiers
Why do we use void in C programming functions, instead of format specifiers? - Quora
Answer (1 of 7): Void is one of those language smells in C. They tried to make everything functions, but not variables and constants which are also functions. Routines that donโ€™t return anything are not functions. C is really confused over functions. https://www.quora.com/Does-a-function-always-...