Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where does the return value of called fn go when ignored?

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?!

like image 969
Vaibhav Bajpai Avatar asked Jul 30 '11 21:07

Vaibhav Bajpai


People also ask

What happens if you don't return value from function?

If no return statement appears in a function definition, control automatically returns to the calling function after the last statement of the called function is executed. In this case, the return value of the called function is undefined.

What happens when you call a function with a return value?

What happens when you call a function (which has return value) without assigning it to anything? -->The function will be executed, either make no sense like your case or make a lot of senses like modifying a static variable or a global variable. The return value will be ignored.

Can a function not have a return statement Python?

In Python, it is possible to compose a function without a return statement. Functions like this are called void, and they return None, Python's special object for "nothing". Here's an example of a void function: >>> def sayhello(who): print('Hello,', who + '!'

How do you ignore a value in Python?

Ignoring ValuesUnderscore(_) is also used to ignore the values. If you don't want to use specific values while unpacking, just assign that value to underscore(_). Ignoring means assigning the values to special variable underscore(_). We're assigning the values to underscore(_) given not using that in future code.


2 Answers

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.

like image 56
Lasse V. Karlsen Avatar answered Nov 01 '22 00:11

Lasse V. Karlsen


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 goes into the bit bucket. wikipedia article: http://en.wikipedia.org/wiki/Bit_bucket

An example:

std::set<std::string> set_of_strings;
...
set_of_strings.insert (some_string);

Here std::set::insert() returns a std::pair, an iterator pointing to the element in the set and a bool indicating whether an element was added. In this case that returned std::pair is just going to disappear. In many cases you just don't care whether an element with the same value was already present. In such cases there is no reason to check the second return value. In many cases you don't care about the iterator, either. So just let it go away.

Some overly pedantic programmers will type the above like this:

std::set<std::string> set_of_strings;
...
(void) set_of_strings.insert (some_string);

The (void) supposedly tells the compiler to ignore the return result. However, the compiler doesn't need to be told to do that. It will ignore the return result if it is not used. What the (void) supposed does do is to tell readers of the code that the return value is intentionally being ignored.

However, you would never do this:

(void) x = 42.0;
(void) a = b = c = 42;

Both of those assignment statements return a value. (You wouldn't be able to say a = b = c = 42 if it didn't.) This is just but one of many examples showing that you send data to the bit bucket all the time without knowing it.

My recommendation: Don't use (void) to announce that you are intentionally ignoring the return result. The compiler doesn't need it, a reasonable reader of the code doesn't needed, and it just makes the programmer who wrote it look like an arrogant know-it-all who most likely knows a lot less than they think they do.

like image 35
David Hammen Avatar answered Nov 01 '22 00:11

David Hammen