In Groovy, a function can be defined using the keyword 'def' followed by the function name, parameters (if any), and the body of the function enclosed in curly braces. For example, a simple function that takes two parameters and returns their sum can be defined as follows:
def sum(int a, int b) { return a + b }
Functions in Groovy can also have optional return types specified after the parameter list, similar to Java. Additionally, Groovy allows for more dynamic and flexible function definitions compared to Java, as it supports features like closures and optional typing.
How to define a function in Groovy with default parameter values?
In Groovy, you can define a function with default parameter values by using the following syntax:
1 2 3 |
def myFunction(param1 = defaultValue1, param2 = defaultValue2) { // function body here } |
In this example, param1
and param2
are the parameters of the function myFunction
, and defaultValue1
and defaultValue2
are the default values assigned to these parameters if no value is provided when calling the function.
Here's an example illustrating how to define a function with default parameter values in Groovy:
1 2 3 4 5 6 |
def greet(name = "World") { println "Hello, ${name}!" } greet("Alice") // Output: Hello, Alice! greet() // Output: Hello, World! |
In this example, the greet
function has a default parameter value of "World"
. When we call greet("Alice")
, it will print "Hello, Alice!". When we call greet()
without providing any arguments, it will use the default value and print "Hello, World!".
What is a default argument in Groovy function?
A default argument in a Groovy function is a parameter that is assigned a default value in case no value is provided for it when the function is called. This allows the function to be called with fewer arguments than specified in its definition, as the default value will be used for any missing arguments.
How to define a curried function in Groovy?
In Groovy, a curried function is a function that takes multiple arguments and returns a series of functions, each taking one argument. This allows for partial application of the function.
To define a curried function in Groovy, you can use the curry()
method on a closure. Here is an example:
1 2 3 4 5 6 7 8 |
def add = { a, b -> a + b } def curriedAdd = add.curry() def add2 = curriedAdd(2) def result = add2(3) println(result) // Output: 5 |
In this example, the add
closure is defined to take two arguments and add them together. The curry()
method is then called on the add
closure to create a curried version of the function called curriedAdd
. This curried function can then be called with one argument at a time, as shown in the example above.
How to define a function in Groovy that overrides a parent class function?
In Groovy, you can define a function that overrides a parent class function by using the @Override
annotation. Here is an example of how to define a function in Groovy that overrides a parent class function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
class Parent { void myFunction() { println("Parent class function") } } class Child extends Parent { @Override void myFunction() { println("Child class function") } } def child = new Child() child.myFunction() |
In this example, the Child
class extends the Parent
class and overrides the myFunction
function. The @Override
annotation indicates that the myFunction
function in the Child
class is overriding the myFunction
function in the Parent
class. When the myFunction
function is called on an instance of the Child
class, it will print "Child class function" instead of "Parent class function".
How to define a recursive function in Groovy?
In Groovy, a recursive function can be defined like any other function, but with the addition of a call to itself within the body of the function. Here is an example of a simple recursive function in Groovy that calculates the factorial of a number:
1 2 3 4 5 6 7 8 9 |
def factorial(n) { if (n == 0) { return 1 } else { return n * factorial(n - 1) } } println factorial(5) // Output: 120 |
In this example, the factorial
function calls itself with factorial(n - 1)
until n
becomes 0, at which point it returns 1 to stop the recursion.