def (function)
Try it yourself →Defines your own command.
Syntax
def name(params) { ... return ... }Description
Defined with def. return returns the result; the last expression is also automatically returned (return is optional).
Example
def square(x) {
return x * x
}
println square(5)Expected output
25
When to use?
Preventing code repetition, breaking the program into parts.