🌐
Stack Overflow
stackoverflow.com › questions › 37523492 › calling-of-a-void-function-in-c
Calling of a VOID function in C - Stack Overflow
The calling in MAIN is inside a switch case. void print_all(char warehouse[][M], float price[], int n) { printf("\n\n"); int m=0, p=0; for (m=0; m<n; m++) { for (p=0; p<M; p++) { printf("%c TEST", warehouse[m][p]); } printf(" %.2f Euros\n", ...
🌐
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 › 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.
🌐
Yale University
cs.yale.edu › homes › aspnes › pinewiki › C(2f)Functions.html
C/Functions
A function call consists of the ... call that returns a value can be used in an expression just like a variable. A call to a void function can only be used as an expression by itself:...
🌐
Cprogramming
cboard.cprogramming.com › c-programming › 178803-void-before-function-call.html
void before function call
The problem is that void tom() is a function declaration not a cast. A good properly configured compiler would be at least warning you about the issues, and probably even emitting an error or two. ||=== Build: Debug in chomework (compiler: GNU GCC Compiler) ===| main.c||In function ‘tom’:| main.c|4|error: implicit declaration of function ‘printf’ [-Wimplicit-function-declaration]| main.c|4|warning: incompatible implicit declaration of built-in function ‘printf’| main.c|4|note: include ‘<stdio.h>’ or provide a declaration of ‘printf’| main.c||In function ‘main’:| main.c|
🌐
W3Schools
w3schools.com › c › c_functions.php
C Functions
In the following example, myFunction() is used to print a text (the action), when it is called: ... // Create a function void myFunction() { printf("I just got executed!"); } int main() { myFunction(); // call the function return 0; } // Outputs "I just got executed!" Try it Yourself »
🌐
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.
Top answer
1 of 2
1

Let's make it more clear first. You have a void function, called mark_function, which takes 2 parameters. The first parameter is a void pointer and the second parameter is a pointer to a function that returns void and takes as a parameter a void pointer. Let's create a function that will be apropiate to pass as parameter to mark_function.

void param(void *p) {
    // function body
}

Assume a and b are 2 void pointers. I will not enter into details about their scope, but you need to pay attention to it: they must be available in the scope they are used. Then, the mark_function will be called as:

mark_function(a, param);

Inside mark_function body you can have something like:

param(b);

, which is a call to the function passed as a parameter.


Long story short: the function pointers used as parameters are meant to make it possible for the function that requires them to perform different activities by calling different functions. The value of a pointer function is simply the name of a function that has the appropiate signature (return value and parameter list). Your function can use this to call the function provided as parameter as needed.

2 of 2
0

You should call the function with one pointer to an object, and one pointer to a function of suitable type.

int my_int;

void mark_function(void *obj, void (*mark_obj)(void *));

void my_func (void *vp)
{
  /* Convert vp to a pointer to a real type */
  int *ip = vp;

  /* Do something with ip or *ip */
}

mark_function (&my_int, my_func);

A pointer to object (&my_int) can safely be convert to a void * and back to the same type. The resulting pointer is guaranteed to be identical to the original pointer. This allows mark_function() to perform some task regardless of the actual type that the pointer points to.

Typical examples of such tasks include sorting of arrays with a custom compare function provided by the caller, or maintaining linked lists without knowing the types of the object stored inside, an so on.

🌐
E Computer Notes
ecomputernotes.com › home › pointer › void functions in c
Void Functions in C - Computer Notes
October 31, 2020 - Program presents an example where a void function is defined to display a message. Illustrates a void function with void parameter list. #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"); printf("Also remember \"It is practice which makes a man/woman perfect in programming in C.\"\n"); }
Find elsewhere
🌐
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
🌐
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
🌐
Cplusplus
cplusplus.com › forum › beginner › 184240
Help calling a void function. - C++ Forum
Exactly what do you need help with? If you want to call the function you simply write line(width, fill_char);
🌐
YouTube
youtube.com › watch
Legal Ways To Call Void Functions | C Programming Tutorial
Enjoy the videos and music you love, upload original content, and share it all with friends, family, and the world on YouTube.
🌐
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.
🌐
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 - These functions can carry out all ... call these functions “void functions”. To define a void function, we should start with the keyword void which represents this function has no possible return value....
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.

🌐
Cplusplus
cplusplus.com › forum › general › 264124
Calling a void function with user input - C++ Forum
void functions just do not return a value. that is all void means. some void functions use a reference parameter to hold any results. Some do not have results to return (print functions, for example). the difference: void foo(int x); int bar (int y); .. z = bar(3); //bar returns values. bar (4); //but you can ignore the return value and throw it away in c++ foo(4); //call a void function.
🌐
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!
But sometime function does not need to return anything so in that case we use the keyword "void" to indicate the return type. eg: // here we return the sum of a and b // so we have int return type // also you can see keyword "return" inside ...