help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

avoiding copying arguments by value when calling a function


From: Sergei Steshenko
Subject: avoiding copying arguments by value when calling a function
Date: Mon, 20 Aug 2012 06:55:43 -0700 (PDT)

Hello,

if I understand correctly, if I write a function like this:

function result = foo(bar)
  # function body goes here
endfunction

and if I call the function this way

result = foo(bar);

, the 'bar' argument is copied by value each time 'foo' is called.


I was thinking of something like this:


bar_func = @() bar; # Octave accepts this

function result = foo(bar_func)
  # function body goes here
  # whenever I need 'bar' I use bar_func() instead
endfunction

- I tried this and it works, i.e. produces the expected result.

If again I understand correctly, passing 'bar_func' is passing a function 
handle, which for large enough 'bar' data can hopefully yield better 
performance.

Are my understandings correct ? I.e. can anonymous functions be used as data 
wrappers in order to avoid physical copying of data ?

Thanks,
  Sergei.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]