Dear Mrs. and Mr. Forum,
Jacob Hirbawi asks
What happens when you call a function that returns a value, but yet you
don't assign that returned value to anything?For example in :
test:=function(n) local a; a:=1; List([1..n]); <---- return a; end;where does the returned value of List([1..n]) "go"?
Since this value is not bound to a variable, it is garbage and thus
will be thrown away automatically by the next call of the garbage collector.
Let us look at an example.
(Of course it is not necessary to force garbage collections,
here this is done just to look at the used/free space.)
gap> # Switch on the messages about garbage collections. gap> GASMAN( "message" ); gap> # Define the function. gap> test:= function( n ) > # Produce a large object for large value of 'n'. > # (The command 'List( [ 1 .. n ] )' would return > # a range, whose memory size does not depend on 'n'.) > List( [ 1 .. n ], x -> 0 ); > end;; gap> # Collect existing garbage. gap> GASMAN( "collect" ); #G collect garbage, 3010 used, 459 dead, 2962 KB free, 4096 KB total gap> # Produce new garbage, gap> # and look what remains after the next garbage collection. gap> test( 10^4 ); GASMAN( "collect" ); #G collect garbage, 4080 used, 10509 dead, 2906 KB free, 4096 KB total gap> test( 10^4 ); GASMAN( "collect" ); #G collect garbage, 4080 used, 10011 dead, 2906 KB free, 4096 KB total gap> test( 10^4 ); gap> test( 10^4 ); GASMAN( "collect" ); #G collect garbage, 4080 used, 20019 dead, 2906 KB free, 4096 KB total
So after each call of the function 'test' the garbage collector finds
about 10000 ``dead'' objects, namely the list with 10000 zeros.
Kind regards
Thomas Breuer