I just saw a code snippet in Dive into Python where a function was calling another function (that actually returns something other than None) and still the calling function does NOT assign the returned value to a variable.
I got alarmed by it and quickly went to try it:
>>> def foo(): return "hello_world!"
...
>>> def bar(): foo()
...
I realize every function in python returns (either None or something else)
To my surprise when I tried the same logic in my previously learned languages, they seem to show similar behavior:
C:
#include<stdio.h>
char* foo(){return "hello_world!";}
int main(void){
foo(); // works!
return 0;
}
C++
#include<iostream>
#include<string>
using namespace std;
string foo(){return "hello_world!";}
int main(){
foo(); // works!
return 0;
}
Java:
public class Test{
public static String foo(){return "hello_world!";}
public static void main(String args[]){
foo(); // works!
System.exit(0);
}
}
All this time I was assuming if a function actually returns something, it SHOULD be set to some variable, otherwise where would the returned value go?!
It disappears.
In case of languages and runtimes in which an object is returned that would need to be garbage collected, the result of calling that function is eligible for collection as soon as the function returns.
In case of languages and runtimes that return an object that is reference counted, or similarly protected, to force an immediate cleanup when the object is no longer needed, that cleanup will occur when the function has returned.
Otherwise, for all intents and purposes, the value is just lost. There’s no harm done and should be perfectly safe to do.