HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to ScalaVariables (val / var)Data TypesStringsListsConditions (if / match)Loops (for / while)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables & Types
🔢 Numbers & Math
✍️ String
📚 Collection
🔀 Condition & Loop
🧩 Function
Scala›Functions

Functions

Function definition with def, parameters, return type, and anonymous functions.

Simple enough for a 5-year-old to understand

Define and call functions

Example code
1
def greet(name: String): Unit =
Unit: does not return a value.
FunctionUnit: does not return a value.
2
println(s"Hello $name!")
Uses the parameter.
Output / InputUses the parameter.
3
greet("Alex")
Calls the function.
FunctionCalls the function.
FunctionOutput / Input
💡 Hint: It is mandatory to write the parameter type.

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.Which keyword is used to define a function in Scala?

About this topic

Methods in Scala are defined with def. An equals sign precedes the body, and the last evaluated expression becomes the return value; there is no need to write a separate return statement.

Functions in Scala are first-class values: they can be held in a variable, passed as arguments, and returned. All collection methods are built upon this feature.

Parameters with default values and named arguments largely eliminate the need to write separate methods for a few variations of the same task. The overloading clutter that Java developers are used to is much less in Scala.

What you will learn

  • Defining methods
  • How the return value is determined
  • Default and named arguments
  • Using functions as values
  • Empty return type

Who is this for?

  • Beginners to Scala
  • Those moving from Java and writing overloading
  • Those who receive an empty value while expecting a return value

When to use?

If a piece of code is being written a second time, it should be extracted into a method.

If the same task requires a few parameter variations, parameters with default values produce less code than writing separate methods.

Common mistakes

Wrong

def topla(a: Int, b: Int): Int = {
  println(a + b)
}

The method declared that it would return an integer, but the last expression of the body is printing to the screen; this operation returns an empty value and a type mismatch error is thrown. Printing and returning are different things.

Correct

def topla(a: Int, b: Int): Int = {
  a + b
}

Wrong

def kare(x: Int) {
  x * x
}
println(kare(5))

Since there is no equals sign, the body is considered written with the old procedure syntax and the method returns an empty value; the calculated value is discarded. This syntax has been removed in newer Scala versions, but it is common in legacy code and is a very sneaky bug.

Correct

def kare(x: Int): Int = {
  x * x
}
println(kare(5))

Wrong

def artir(sayi: Int): Int = {
  sayi = sayi + 1
  sayi
}

Parameters are like val and cannot be reassigned; a "Reassignment to val" error is thrown. This prevents the caller's data from changing unexpectedly. Producing and returning a new value is the correct way.

Correct

def artir(sayi: Int): Int = {
  sayi + 1
}

Frequently asked questions

Should I write the return type?

It is not needed for local helpers, type inference is sufficient. It is recommended to write it for public methods: it serves both as documentation and is mandatory for recursive methods, because the compiler cannot infer the type.

What is the difference between a method and a function?

A method belongs to a class or object and is defined with def; a function is a value and can be held in a variable. Scala converts a method to a function when necessary, so the difference is rarely felt in daily use.

When is a named argument used?

When there are multiple parameters of the same type, or when you want to skip middle parameters and provide only one. It makes what the call does readable and eliminates ordering errors.

Summary

Methods are defined with def.

The equals sign before the body is mandatory.

The last expression becomes the return value.

Parameters cannot be reassigned.

Functions can be passed as values.

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