In C language, when to use the void function? how to use it? Can you give me some examples. I just need to understand it. Thanks!
when to use void function: When u don't want a function to return anything then use void function Example #include<stdio.h> void function(int a,int b) { int x=a+b; printf("The value of x is "); printf("%d",x); } int main() { int x=10,y=20; function(10,20); }
#include<stdio.h> int function(int a,int b) { int x=a+b; return x; } int main() { int x=10,y=20; b=function(10,20); printf("The value of b is"); printf("%d",b); } output of this program is- the value of b is 30 In this program function is returning the value of x to main function When ever u dont want function to return anything then use void function
Please feel free to contact if u have any doubt
you make a function void if it doesnt have to return any values.
you can use 'return' values from functions to return values computed or looked-up from the function, or to report error/success states, etc. "Void" means "nothing" and is used when you don't need your function to return anything.
oh okay! thank you guys! :)
Just a little tidbit: A lot of functions with no return are actually boolean types. They may simply return true if they succeed and false if they fail. So void is used a lot less than you might think.
thanks for that additional input :)
Join our real-time social learning platform and learn together with your friends!