Procedures and reporters with default valued arguments in NetLogo

Viewed 51

I am new to NetLogo but coming from previous functional programming languages.

Is there a way to have default values in procedures and reporters? or the norm is to set up global variables for parameters?

1 Answers

Sadly, no, there is no built-in way to provide default values for procedure arguments, command or reporter. There is at least one alternative I can think of that might provide some of the benefit, but it's not quite the same.

You didn't provide code for what you're doing, so I'll make a toy example:

to setup
  clear-all
  make-colony (one-of patches with [pcolor = black]) 10 red 1
  make-colony (one-of patches with [pcolor = black]) 10 blue 1
  make-colony (one-of patches with [pcolor = black]) 10 green 1
  make-colony (one-of patches with [pcolor = black]) 10 orange 1
  make-colony (one-of patches with [pcolor = black]) 10 violet 1
  make-colony (one-of patches with [pcolor = black]) 10 yellow 1
  reset-ticks
end

to make-colony [colony-patch ant-count colony-color ant-size]
  ask colony-patch [
    set pcolor colony-color
    sprout ant-count [
      set shape "bug"
      set color colony-color
      set size ant-size
      fd 1
    ]
  ]
end

You can "store" the default values using anonymous procedures, but that anonymous procedure will have a fixed number of arguments it takes for the non-default values. Below I have the "regular" method from the starting example, but I build an anonymous procedure that provides the common arguments except for the color. You could store that make-regular-colony anonymous procedure in a global variable if you wanted to use it in multiple locations in code, you'd just have to make sure it's always set before running that code (setup after the clear-all is a good place).

to setup
  clear-all
  let make-regular-colony [ c -> make-colony (one-of patches with [pcolor = black]) 10 c 1 ]
  (run make-regular-colony red)
  (run make-regular-colony blue)
  (run make-regular-colony green)
  (run make-regular-colony orange)
  (run make-regular-colony violet)
  (run make-regular-colony yellow)
  reset-ticks
end

to make-colony [colony-patch ant-count colony-color ant-size]
  ask colony-patch [
    set pcolor colony-color
    sprout ant-count [
      set shape "bug"
      set color colony-color
      set size ant-size
      fd 1
    ]
  ]
end

This example isn't great, because you could just use foreach like foreach [red blue green orange violet yellow] [ c -> make-colony ... ] to get the same effect, but hopefully it makes the idea clear.

Related