What is the logic behind "void"? pls can anyone explain | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

What is the logic behind "void"? pls can anyone explain

2nd Sep 2017, 7:22 PM
Bhupesh Neolia
Bhupesh Neolia - avatar
13 Answers
+ 5
It's used for declaring a function or method which won't return anything at all
2nd Sep 2017, 7:33 PM
Dapper Mink
Dapper Mink - avatar
+ 5
alternatively there coulb be nothing. But with void, you declare not to have forgotten to describe return parameter.
2nd Sep 2017, 7:35 PM
Oma Falk
Oma Falk - avatar
+ 5
@Andrew that actually starting to make sense to me. thanks for your superb explanation ;) and you sure know our heroes :p я из Германии~
26th Sep 2017, 4:54 PM
アーッChebu❗
アーッChebu❗ - avatar
+ 4
If you want to know what it does, here's my explanation: void is a keyword for a method, e.g. such as int. It says that the method wont return anything
2nd Sep 2017, 7:26 PM
Elias
Elias - avatar
+ 4
is there something like void in python, too? I don't quite understand the concept of a method which doesn't return anything at all
2nd Sep 2017, 7:39 PM
アーッChebu❗
アーッChebu❗ - avatar
+ 4
the urgent question is, why do I need a method/function which doesn't return anything at all
2nd Sep 2017, 8:31 PM
アーッChebu❗
アーッChebu❗ - avatar
+ 4
You can also use void* as return type for a function returning a pointer to an unknown value.
2nd Sep 2017, 9:53 PM
Karl T.
Karl T. - avatar
+ 3
You don't need to specify any data type in Python which makes it easier somehow
2nd Sep 2017, 7:43 PM
Dapper Mink
Dapper Mink - avatar
+ 3
@Cheburashka It's in functional programming (paradigm) any function must get something and return something too not alteringby the way anything outside (of this function itself) inside its body. But in other programming paradigms it's ok to have such functions, for example to change and print something, not returning it. In pseudocode for example: ... function showIncrementedValue (x) { print (x+1); //and it can alter some global variable: "globalCounterVar += 1;" //and no return here } //so you can run it without getting any value from it //(not "a = showIncrementedValue;", but see further) value = 9; showIncrementedValue(value); /*so output will be 10, but function doesn't return anything to the programm flow at the point, where it was called.*/ Btw, Cheburashka, where are you from?? :)
26th Sep 2017, 4:50 PM
Andrew Harchenko (Tomsk)
Andrew Harchenko (Tomsk) - avatar
+ 2
For instance: avoid repetitions of lines of code
2nd Sep 2017, 8:34 PM
Dapper Mink
Dapper Mink - avatar
+ 1
Yes. In Python you just write a function without the return keyword.
2nd Sep 2017, 8:29 PM
1of3
1of3 - avatar
+ 1
if you write a setter-method for example, there is no need for a return value. All happens inside of the method.
3rd Sep 2017, 8:14 AM
KenAc
KenAc - avatar
0
Print, log or whatever a function that prints to the screen is called, is a typical example of a non-returning function. Same with operations that write to files, copy to clipboard or any other kind of output.
3rd Sep 2017, 10:47 AM
1of3
1of3 - avatar