In many other programming languages, you can pass a function as an argument to another function and call it from within the function.
Is there anyway to do this in Netlogo?
Such as the following:
;; x,y,z are all ints
to-report f [x y z]
  report x + y + z
end
;; some-function is a function
;; x y and z are ints
to-report g [some-function x y z]
  report (some-function x y z) + 2
end
to go
  show g f 1 2 3
end
This would be a nice feature. I'm trying to implement an abstract local search algorithm which this would be nice for passing in objective functions and such.
As of Netlogo 6.0.1, the arrow syntax replaced tasks. The below does the same thing as the accepted answer but with the updated syntax.
to-report f [x y z]
  report x + y + z
end
;; some-function is a function
;; x y and z are ints
to-report g [some-function x y z]
  report (runresult some-function x y (z + 2))
end
to go
  show g [[x y z] -> (f x y z)] 1 2 3
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With