HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to GroovyVariablesData TypesOperatorsStringsListsConditions (if / else)Loops (for / while)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables & Types
🔢 Numbers & Math
✍️ String
📚 List & Map
🔀 Condition & Loop
🧩 Function
Groovy›Functions

Functions

Function definition with def, parameters, return, and calling.

Simple enough for a 5-year-old to understand

Define a function

defined with def. return gives back the result (the last expression is also returned automatically).

Example code
1
def square(x) {
Function that takes x.
FunctionFunction that takes x.
2
return x * x
Returns the result.
DefinitionReturns the result.
3
}
4
println square(5)
25
Output / Input25
FunctionDefinitionOutput / Input
💡 Hint: return gives back a value; println only prints to the screen.

About this topic

Methods in Groovy are defined with def or by writing the return type. The last evaluated expression is returned automatically; writing a return statement is optional.

Closures are Groovy's most characteristic feature: they are code blocks written in curly braces that can be stored in variables and passed as arguments. All collection methods take closures.

The distinction between a method and a closure is important. A closure is an object and requires a special syntax to be called; a method belongs to a class. Confusing the two is a common mistake for beginners.

What you will learn

  • Defining methods
  • Implicit return behavior
  • Closures
  • Default arguments
  • Named argument pattern

Who is this for?

  • Those who want to organize their scripts
  • Those learning the concept of closures
  • Those writing Gradle task definitions

When to use?

Repeated steps in build scripts should be extracted into a method; copied blocks diverge from each other over time.

If you need to pass a behavior as a parameter, closures are used; Gradle task configurations rely on this mechanism.

Common mistakes

Wrong

def kare = { x -> x * x }
println kare(5)

This code actually works because Groovy allows a short call form for closures; however, the explicit call method is clearer and makes the difference between a closure and a method visible. Confusion arises when both a method and a closure are defined with the same name.

Correct

def kare = { x -> x * x }
println kare.call(5)

Wrong

def topla(a, b) {
  println a + b
}
def sonuc = topla(2, 3)
println sonuc

The method prints to the screen but does not return a value; since the print operation produces a void value, the result comes out empty. Printing and returning are different things, and Groovy's implicit return returns an unintended value here.

Correct

def topla(a, b) {
  a + b
}
def sonuc = topla(2, 3)
println sonuc

Wrong

def selamla(selam = "Merhaba", ad) {
  println "$selam $ad"
}
selamla("Ayşe")

When a parameter with a default value is placed first, a single-argument call becomes ambiguous and the value goes to the wrong parameter. Parameters with default values must always be placed at the end; this rule applies in almost all languages.

Correct

def selamla(ad, selam = "Merhaba") {
  println "$selam $ad"
}
selamla("Ayşe")

Frequently asked questions

What is the difference between a closure and a method?

A closure is an object: it can be stored in variables, passed around, and created at runtime. A method belongs to a class and is defined at compile time. The blocks you pass to collection methods are closures.

How are named arguments used?

There is no separate syntax in Groovy; the first parameter is defined as a map and key-value pairs are written when calling. It significantly increases readability in multi-parameter configuration methods.

Should I write a return statement?

It is not mandatory, but in long methods and when there are conditional branches, writing it explicitly clarifies which value is returned. Implicit return can lead to silent errors if the last expression produces an unexpected value.

Summary

The last expression is returned automatically.

The print operation produces a void value.

Closures are code blocks that can be stored in variables.

Parameters with default values must be placed at the end.

Named arguments are provided with a map.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code