+ 2
What is the logic behind "void"? pls can anyone explain
13 odpowiedzi
+ 5
It's used for declaring a function or method which won't return anything at all
+ 5
alternatively there coulb be nothing. But with void, you declare not to have forgotten to describe return parameter.
+ 5
@Andrew that actually starting to make sense to me. thanks for your superb explanation ;)
and you sure know our heroes :p
я из Германии~
+ 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
+ 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
+ 4
the urgent question is, why do I need a method/function which doesn't return anything at all
+ 4
You can also use void* as return type for a function returning a pointer to an unknown value.
+ 3
You don't need to specify any data type in Python which makes it easier somehow
+ 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?? :)
+ 2
For instance: avoid repetitions of lines of code
+ 1
Yes. In Python you just write a function without the return keyword.
+ 1
if you write a setter-method for example, there is no need for a return value. All happens inside of the method.
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.