๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 37523492 โ€บ calling-of-a-void-function-in-c
Calling of a VOID function in C - Stack Overflow
This is the function that I have to print. 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", price[m]); } }
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
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|
๐ŸŒ
Florida State University
cs.fsu.edu โ€บ ~cop3014p โ€บ lectures โ€บ ch7 โ€บ index.html
Functions 2: Void (NonValue-Returning) Functions
Call to void function with empty formal parameter list: ... Reference Parameter: formal parameter receives location (memory address) of corresponding actual parameter Using Value and Reference Parameters: Value Parameters: Value of corresponding actual parameter copied into formal parameter
๐ŸŒ
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"); }
๐ŸŒ
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
๐ŸŒ
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.
Find elsewhere
๐ŸŒ
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);
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/cpp_questions โ€บ help with parameters, void function, calling functions, and returns
r/cpp_questions on Reddit: Help with parameters, void function, calling functions, and returns
January 19, 2023 -

Hello,

I am a beginner at coding and I am currently learning C++ as my first language. I started 3 days ago, and I am using Codecademy to learn. Right now I am doing a subsection "Parameters and Arguments" in the section "Functions." The way I was taught to use the return function is as follows in the code that they gave me:

#include <iostream>

bool morning = true;

std::string make_sandwich(){
   
    std::string sandwich = "";
    sandwich += "bread\n";
    if (morning == true) {
        sandwich += "egg\n;
    }
    sandwich += "cheese\n";
    sandwich += "bread\n";
    return sandwich;
    
    }

    int main {
       
        std::cout << "Your sandwich now:\n" << make_sandwich();
    }

This was the previous lesson before I was given an assignment, and I have been trying for a long time to do it. It told me: "Let's build a function that prints out the current emergency number, whatever it is from now on. Above `main()`, define a `void` function `get_emergency_number()` that accepts one parameter: a string with the name `emergency_number`."

There are some other steps, but I cannot proceed until I have finished this one.

The code (without // comments [it is kind of hard to understand the comments because it is also for other steps]) is as follows:

#include <iostream>

int main() {

    int emergency_number;

        std::string old_emergency_number = "999";

        std::string new_emergency_number = "0118 999 991 999 725 3";

}

This is my most recent try:

#include <iostream>

void get_emergency_number() {
    std::string int emergency_number;
}

int main() {

    int emergency_number;

        std::string old_emergency_number = "999";

        std::string new_emergency_number = "0118 999 991 999 725 3";

}

I had many, many more tries, but I could not get this to work, I even had a somewhat-experienced coder look at the code, but they could not figure it out.

Please help me and do not just fix, I would prefer an explanation that will serve good use to me for my knowledge.

Thank you,

Zach

Top answer
1 of 5
6
define a void function get_emergency_number() that accepts one parameter: a string with the name emergency_number." void get_emergency_number(std::string emergency_number) { } A function with get in its name that doesn't return anything is confusing. Have you considered : https://www.learncpp.com/cpp-tutorial/introduction-to-function-parameters-and-arguments/
2 of 5
3
Hint: what is the difference between these *signatures* (free vocab lesson for the day)? void print_number(); void print_number(std::string); void print_number(const std::string&); The first signature takes no arguments. So it does not do what you want. The other signatures take one argument, and they are much closer to what I would expect you want. Why? This version is a very reasonable first attempt: void print_number(std::string); It takes a single std::string argument by value. There are potential performance implications for your program because the compiler is required to give that function call its own unique instance of its argument, which potentially involves making a copy. This version void print_number(const std::string&); is better for a few reasons: It passes by reference which means that the original string object is shared It also tells the compiler "inside the scope of this function, treat this variable as constant and make any attempt to modify it an error". Today you probably don't care too much about this, but long term "const correctness" is very powerful tool for avoiding entire categories of bugs. So let's start with #2. Your code skeleton should look something like this now: void print_number(std::string number) { // Your implementation goes here } std::string get_number() { // Your implementation goes here return "1 2 3 4 5"; } int main(int, const char**) { const std::string new_number = get_number(); print_number(new_number); return 0; } Hope this helps.
๐ŸŒ
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.
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.

๐ŸŒ
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
๐ŸŒ
ThoughtCo
thoughtco.com โ€บ definition-of-void-958182
What Is the Definition of "Void" in C and C++?
April 28, 2019 - The void function call is a stand-alone statement. 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.
๐ŸŒ
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 ...
๐ŸŒ
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
๐ŸŒ
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.