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

Functions

function...end block, single-line definition (f(x)=...), return and calling.

Simple enough for a 5-year-old to understand

Define a function

Functions are defined with function ... end. return returns the result (the last expression is also returned automatically).

Example code
1
function square(x)
Function that takes x.
FunctionFunction that takes x.
2
return x * x
Returns the result.
DefinitionReturns the result.
3
end
4
println(square(5))
25
Output / Input25
FunctionDefinitionOutput / Input
💡 Hint: global is not needed inside functions; they read variables defined outside.

About this topic

In Julia, functions are defined in two ways: the keyword-based long form and the single-line short form. The short form resembles a mathematical definition and is preferred for simple functions.

Julia's most unique feature is multiple dispatch. You can define multiple functions with the same name and have each accept different argument types. At call time, which version to run is chosen by looking at the types of all arguments.

By convention, functions that modify their arguments end with an exclamation mark. The language does not enforce this, but the standard library follows this rule and makes side-effecting parts of the code visible.

What you will learn

  • The two ways of defining functions
  • Multiple dispatch
  • The modifying function convention
  • Keyword arguments
  • Return values

Who is this for?

  • Beginners to Julia
  • Learners of multiple dispatch
  • Those surprised by argument modification

When to use?

When the same operation needs different implementations for different types, multiple dispatch is both faster and more readable than writing conditionals.

All performance-critical code must be inside functions; code running in the global scope cannot benefit from compiler optimizations.

Common mistakes

Wrong

function ekle(dizi)
    push!(dizi, 99)
end
a = [1, 2, 3]
ekle(a)
println(length(a))

No error here, but the result might be surprising: since arrays are passed by reference, the function modified the caller's data and the length became four. Julia expects you to signal this by putting an exclamation mark at the end of the function name; otherwise, the reader won't notice the side effect.

Correct

function ekle!(dizi)
    push!(dizi, 99)
end
a = [1, 2, 3]
ekle!(a)
println(length(a))

Wrong

function topla(a, b)
    a + b
end
println(topla(2))

A function expecting two arguments was called with a single argument; Julia throws a "no method matching topla(::Int64)" error. Due to multiple dispatch, the error message also lists which signatures are available, making it easier to find the problem.

Correct

function topla(a, b = 0)
    a + b
end
println(topla(2))

Wrong

function bol(a, b)
    if b == 0
        println("division by zero")
    end
    return a / b
end
println(bol(10, 0))

A warning is printed, but the function still performs the division and returns infinity. Because an early return was not made, the check does nothing. In an error state, you should either return early or throw an error.

Correct

function bol(a, b)
    if b == 0
        error("division by zero")
    end
    return a / b
end

Frequently asked questions

How does multiple dispatch work?

You define separate versions of a function with the same name for different argument types; the most appropriate version is selected at call time. Unlike single-object selection in object-oriented languages, all arguments are taken into account.

Is writing return mandatory?

Not mandatory; the last evaluated expression is returned. Still, writing it explicitly improves readability in long functions and when early returns are needed. It is important not to leave the returned value ambiguous.

How are keyword arguments defined?

They are written after a semicolon in the argument list and must be provided by name when called. They significantly improve readability in functions that accept a large number of optional settings.

Summary

Functions can be defined in long and short forms.

Multiple dispatch looks at all argument types.

Modifying functions end with an exclamation mark.

Arrays are passed by reference to the function.

The last expression is returned automatically.

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