Les funcions buides es coneixen com a Funcions de retorn sense valor . Són "buits" degut al fet que no se suposa que retornin valors. És cert, però no del tot. No podem retornar valors, però hi ha alguna cosa que segurament podem tornar de les funcions de buit. Les funcions de void no tenen un tipus de retorn, però poden fer valors de retorn. Alguns dels casos es mostren a continuació:
1) Una funció void pot tornar: Simplement podem escriure una declaració de devolució en un void Fun (). De fet, es considera una bona pràctica (per llegir el codi) per escriure un retorn; Declaració per indicar el final de la funció.
// CPP Program to demonstrate void functions #include using namespace std; void fun() { cout << 'Hello'; // We can write return in void return; } // Driver Code int main() { fun(); return 0; }
Producció
Hello
Complexitat del temps: O (1)
Complexitat espacial: O (1)
2) Un void Fun () pot retornar una altra funció buida: Una funció void també pot trucar a una altra funció buida mentre s’acaba. Per exemple
CPP// C++ code to demonstrate void() // returning void() #include using namespace std; // A sample void function void work() { cout << 'The void function has returned ' ' a void() !!! n'; } // Driver void() returning void work() void test() { // Returning void function return work(); } // Driver Code int main() { // Calling void function test(); return 0; }
Producció
The void function has returned a void() !!!
Complexitat del temps: O (1)
Complexitat espacial: O (1)
El codi anterior explica com Void () pot ser útil per retornar funcions de buit sense haver de donar errors.
3) Un void () pot retornar un valor buit: Un void () no pot retornar un valor que es pugui utilitzar. Però pot retornar un valor buit sense donar cap error. Per exemple
// C++ code to demonstrate void() // returning a void value #include using namespace std; // Driver void() returning a void value void test() { cout << 'Hello'; // Returning a void value return (void)'Doesn't Print'; } // Driver Code int main() { test(); return 0; }
Producció
Hello
Complexitat del temps: O (1)
Complexitat espacial: O (1)