๐ŸŒ
Learn C++
learncpp.com โ€บ cpp-tutorial โ€บ void-functions-non-value-returning-functions
2.3 โ€” Void functions (non-value returning functions) โ€“ Learn C++
Therefore, printHi is given a void return type. When main calls printHi, the code in printHi executes, and โ€œHiโ€ is printed. At the end of printHi, control returns to main and the program proceeds. A function that does not return a value is called a non-value returning function (or a void ...
๐ŸŒ
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:
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Operators โ€บ void
void operator - JavaScript | MDN
Arrow function expressions always require parentheses around them when being called. ... When a browser follows a javascript: URI, it evaluates the code in the URI and then replaces the contents of the page with the returned value, unless the returned value is undefined. The void operator can be used to return undefined. For example...
๐ŸŒ
Mtsu
cs.mtsu.edu โ€บ ~cs1170 โ€บ manual โ€บ lab8 โ€บ lab8.html
Lab 8 -- C++ void Functions, Value and Reference Parameters, Local Variables
Example: A function that updates ... the value of the amount is assigned // Postcondition: the value of the balance is updated void UpdateBalance(float amount, float& balance); &nbsp or...
๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ cpp โ€บ cpp โ€บ void-cpp
void (C++) | Microsoft Learn
// void.cpp void return_nothing() { // A void function can have a return with no argument, // or no return statement.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c++ โ€บ return-from-void-functions-in-cpp
Return From Void Functions in C++ - GeeksforGeeks
January 27, 2023 - Some of the cases are listed below: 1) A Void Function Can Return: We can simply write a return statement in a void fun().
๐ŸŒ
Cplusplus
cplusplus.com โ€บ forum โ€บ beginner โ€บ 242392
Use of void function? - C++ Forum
I am beginner and I was wondering why does one use void function? For example in the below code, the message can be printed directly after int main () and hence there is no need to fist run the main function and then go back to void function to print message.
๐ŸŒ
University of Regina
cs.uregina.ca โ€บ Links โ€บ class-info โ€บ 110 โ€บ functions โ€บ void-function.html
Void Functions - Computer Science, University of Regina
The Department of Computer Science celebrated its 50th birthday in 2018. Its first students graduated as the University of Saskatchewan Regina Campus became the University of Regina. Our programs have attracted faculty members and students from all over the world.
๐ŸŒ
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...
Find elsewhere
๐ŸŒ
E Computer Notes
ecomputernotes.com โ€บ home โ€บ pointer โ€บ void functions in c
Void Functions in C - Computer Notes
October 31, 2020 - #include<stdio.h> void Display(void); //Function prototype, void parameter list void main() { Display(); //the function call } //end of main function void Display() { // Definition of Display printf("Play an outdoor game for better health.\n"); ...
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ return-from-void-functions-in-cplusplus
Return from void functions in C++
#include <iostream> using namespace std; void another_func() { cout << "From another_function" << endl; return; } void my_func() { cout << "From my_function" << endl; return another_func(); } int main() { my_func(); return 0; }
๐ŸŒ
Uregina
labs.cs.uregina.ca โ€บ 110 โ€บ functions โ€บ void-function.html
Void Functions
These kind of functions are called void functions. The C++ system includes a standard library - a large collection of prewritten functions, data types, and other items that any C++ programmer may use. To use library functions, you have to place an #include directive near the top of the program, specifying the header file. For example, to use cin and cout, you have to have #include<iostream> in your program; to use sqrt(x) and pow(x, y) functions, you have to have #include<cmath> in your program.
๐ŸŒ
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.
๐ŸŒ
University of Utah
users.cs.utah.edu โ€บ ~germain โ€บ PPS โ€บ Topics โ€บ C_Language โ€บ c_functions.html
C Programming - Functions
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; }
๐ŸŒ
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.
๐ŸŒ
Cplusplus
cplusplus.com โ€บ doc โ€บ tutorial โ€บ functions
Cplusplus
Anyway, this example illustrates how functions can be declared before its definition: The following lines: Declare the prototype of the functions. They already contain all what is necessary to call them, their name, the types of their argument, and their return type (void in this case).
๐ŸŒ
Uregina
labs.cs.uregina.ca โ€บ 110 โ€บ OER2022 โ€บ labs โ€บ lab8 โ€บ void.php
Void Functions - Labs
A void function has a heading that ... identifier/name is preceded by the word void. The parameter list with the corresponding data type is within the parentheses. The body of the function is between the pair of braces. In the above example, void printStars() is the function ...
๐ŸŒ
Wikipedia
en.wikipedia.org โ€บ wiki โ€บ Void_type
Void type - Wikipedia
2 weeks ago - Variables of this type are pointers to data of an unspecified type, so in this context (but not the others) void* acts roughly like a universal or any type; it does not literally "point" to a void in memory. A program can convert a pointer to any type of data (except a function pointer) to a pointer to void and back to the original type without losing information, which makes these pointers useful for polymorphic functions.
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.