๐ŸŒ
W3Schools
w3schools.com โ€บ c โ€บ c_functions.php
C Functions
void means that the function does not have a return value. You will learn more about return values later in the next chapter ยท Inside the function (the body), add code that defines what the function should do ยท Declared functions are not executed ...
๐ŸŒ
W3Schools
w3schools.com โ€บ c โ€บ c_functions_decl.php
C Function Declaration and Definition
You have already learned from the previous chapters that you can create and call a function in the following way: // Create a function void myFunction() { printf("I just got executed!"); } int main() { myFunction(); // call the function return 0; } Try it Yourself ยป
๐ŸŒ
W3Schools
w3schoolsua.github.io โ€บ c โ€บ c_functions_en.html
C Language Tutorial. Functions. Lessons for beginners. W3Schools in English
void means that the function does not have a return value. You will learn more about return values later in the next chapter ยท Inside the function (the body), add code that defines what the function should do ยท Declared functions are not executed ...
๐ŸŒ
W3schools
w3schools.tech โ€บ tutorial โ€บ cprogramming โ€บ c_void_pointer
Void Pointers in C: A Comprehensive Guide for Beginners - Pointers in C - W3schools
So, let's dive in! Imagine you have a magic box that can hold any type of item. That's essentially what a void pointer is in C programming! It's a special type of pointer that can point to data of any type.
๐ŸŒ
W3Schools
w3schools.com โ€บ c โ€บ c_functions_parameters.php
C Function Parameters
Inside the function, you can add as many parameters as you want: void myFunction(char name[], int age) { printf("Hello %s. You are %d years old.\n", name, age); } int main() { myFunction("Liam", 3); myFunction("Jenny", 14); myFunction("Anja", 30); return 0; } // Hello Liam.
๐ŸŒ
W3Schools Blog
w3schools.blog โ€บ home โ€บ c functions
C Functions - W3schools
October 6, 2018 - In order to get the value returned from the function, the function need to be called in the program. Variable_name = function_name (parameters_values); ... #include<stdio.h> int writeMsg() { printf ("My First Program using Function!"); return 0; } void main() { writeMsg(); }
๐ŸŒ
W3Schools
w3schools.com โ€บ c โ€บ c_scope.php
C Variable Scope
A variable created inside a function belongs to the local scope of that function, and can only be used inside that function: void myFunction() { // Local variable that belongs to myFunction int x = 5; // Print the variable x printf("%d", x); } int main() { myFunction(); return 0; } Try it Yourself ยป ยท
๐ŸŒ
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.
๐ŸŒ
W3Schools
w3schools.in โ€บ c-programming โ€บ data-types
C Data Types - W3Schools
Some of them are an integer, floating point, character, etc. Usually, programming languages specify the range values for a given data-type. ... Identify the type of a variable when it is declared. Identify the type of return value of a function. Identify the type of parameter expected by a function. ... Primary(Built-in) Data Types: void, int, char, double, and float.
Find elsewhere
๐ŸŒ
W3Resource
w3resource.com โ€บ c-programming โ€บ c-programming-functions.php
C Programming Functions with Examples
The greet function is declared with void because it doesn't return any value. It takes no arguments, and when called, it simply prints a message. ... In pass by value, a copy of the variable is passed to the function.
๐ŸŒ
ThoughtCo
thoughtco.com โ€บ definition-of-void-958182
What Is the Definition of "Void" in C and C++?
April 28, 2019 - For example, a function that prints a message doesn't return a value. The code in C++ takes the form: ... A void function uses a heading that names the function followed by a pair of parentheses.
๐ŸŒ
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.
๐ŸŒ
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; }
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ cprogramming โ€บ c_void_pointer.htm
void Pointer in C
A void pointer in C is a type of pointer that is not associated with any data type. A void pointer can hold an address of any type and can be typecasted to any type. They are also called general-purpose or generic pointers.
Top answer
1 of 11
253

A pointer to void is a "generic" pointer type. A void * can be converted to any other pointer type without an explicit cast. You cannot dereference a void * or do pointer arithmetic with it; you must convert it to a pointer to a complete data type first.

void * is often used in places where you need to be able to work with different pointer types in the same code. One commonly cited example is the library function qsort:

void qsort(void *base, size_t nmemb, size_t size, 
           int (*compar)(const void *, const void *));

base is the address of an array, nmemb is the number of elements in the array, size is the size of each element, and compar is a pointer to a function that compares two elements of the array. It gets called like so:

int iArr[10];
double dArr[30];
long lArr[50];
...
qsort(iArr, sizeof iArr/sizeof iArr[0], sizeof iArr[0], compareInt);
qsort(dArr, sizeof dArr/sizeof dArr[0], sizeof dArr[0], compareDouble);
qsort(lArr, sizeof lArr/sizeof lArr[0], sizeof lArr[0], compareLong);

The array expressions iArr, dArr, and lArr are implicitly converted from array types to pointer types in the function call, and each is implicitly converted from "pointer to int/double/long" to "pointer to void".

The comparison functions would look something like:

int compareInt(const void *lhs, const void *rhs)
{
  const int *x = lhs;  // convert void * to int * by assignment
  const int *y = rhs;

  if (*x > *y) return 1;
  if (*x == *y) return 0;
  return -1;
}

By accepting void *, qsort can work with arrays of any type.

The disadvantage of using void * is that you throw type safety out the window and into oncoming traffic. There's nothing to protect you from using the wrong comparison routine:

qsort(dArr, sizeof dArr/sizeof dArr[0], sizeof dArr[0], compareInt);

compareInt is expecting its arguments to be pointing to ints, but is actually working with doubles. There's no way to catch this problem at compile time; you'll just wind up with a missorted array.

2 of 11
30

Using a void * means that the function can take a pointer that doesn't need to be a specific type. For example, in socket functions, you have

send(void * pData, int nLength)

this means you can call it in many ways, for example

char * data = "blah";
send(data, strlen(data));

POINT p;
p.x = 1;
p.y = 2;
send(&p, sizeof(POINT));
๐ŸŒ
Wikipedia
en.wikipedia.org โ€บ wiki โ€บ Void_type
Void type - Wikipedia
2 weeks ago - A function with void result type ends either by reaching the end of the function or by executing a return statement with no returned value. The void type may also replace the argument list of a function prototype to indicate that the function takes no arguments.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ void-pointer-c-cpp
void Pointer in C - GeeksforGeeks
A void pointer is a pointer that has no associated data type with it. A void pointer can hold an address of any type and can be typecasted to any type.
Published ย  July 17, 2014
๐ŸŒ
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
๐ŸŒ
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.