painless-functions.asciidoc 1.0 KB

123456789101112131415161718192021222324
  1. [[painless-functions]]
  2. === Functions
  3. A function is a named piece of code comprised of one-to-many statements to
  4. perform a specific task. A function is called multiple times in a single script
  5. to repeat its specific task. A parameter is a named type value available as a
  6. <<painless-variables, variable>> within the statement(s) of a function. A
  7. function specifies zero-to-many parameters, and when a function is called a
  8. value is specified per parameter. An argument is a value passed into a function
  9. at the point of call. A function specifies a return type value, though if the
  10. type is <<void-type, void>> then no value is returned. Any non-void type return
  11. value is available for use within an <<painless-operators, operation>> or is
  12. discarded otherwise.
  13. You can declare functions at the beginning of a Painless script, for example:
  14. [source,painless]
  15. ---------------------------------------------------------
  16. boolean isNegative(def x) { x < 0 }
  17. ...
  18. if (isNegative(someVar)) {
  19. ...
  20. }
  21. ---------------------------------------------------------