ZetCode

Julia Functions Tutorial

last modified November 18, 2026

Functions in Julia are reusable blocks of code that perform specific tasks. They help organize code, improve readability, and reduce redundancy. This tutorial covers basic and advanced usage of functions in Julia with examples that were run with Julia 1.13.

Functions are defined using the function keyword and can accept arguments and return values. Julia supports both named and anonymous functions. Unless an explicit return is used, a function returns the value of its last expression.

Basic Function Definition

This example demonstrates how to define a simple function in Julia.

main.jl
function greet()
    println("Hello there!")
end

greet()

The greet function takes no arguments and prints "Hello there!" when called. The keyword function starts the definition and end closes it; calling the function by name runs its body.

$ julia main.jl
Hello there!

Function with Arguments

This example shows how to define a function that accepts arguments.

main.jl
function add(x, y)
    return x + y
end

println(add(3, 5))

The add function takes two arguments and returns their sum. The return statement ends the function and hands the result back to the caller, where println displays it.

$ julia main.jl
8

Anonymous Functions

This example demonstrates how to define an anonymous function.

main.jl
square = x -> x^2

println(square(7))

The square function squares its input. An anonymous function is written with the -> syntax: the parameters are on the left and the expression to evaluate is on the right. Anonymous functions are useful for short, one-off operations.

$ julia main.jl
49

Multiple Return Values

This example shows how to return multiple values from a function.

main.jl
function divide(x, y)
    return x / y, x % y
end

println(divide(17, 5))

The divide function returns both the quotient and the remainder. Separating the values with a comma builds a Tuple, which is why the result is printed inside parentheses. The tuple can also be destructured, for example with q, r = divide(17, 5).

$ julia main.jl
(3.4, 2)

Default Arguments

This example demonstrates how to define a function with default arguments.

main.jl
function greet(name="Guest")
    println("Hello, $(name)!")
end

greet()
greet("Alice")

The greet function prints "Hello, Guest!" if no argument is provided and uses the supplied name otherwise. The exclamation mark after the interpolated value needs the form $(name)!: written as $name!, Julia would look for a variable named name!, because ! is a valid character in identifiers.

$ julia main.jl
Hello, Guest!
Hello, Alice!

Keyword Arguments

This example shows how to define a function with keyword arguments.

main.jl
function rectangle_area(; length=1, width=1)
    return length * width
end

println(rectangle_area())
println(rectangle_area(length=4, width=5))

The rectangle_area function calculates the area of a rectangle using keyword arguments. The semicolon in the signature separates them from positional arguments. Keyword arguments can be supplied by name in any order, or left out to use their defaults.

$ julia main.jl
1
20

Variable Number of Arguments

This example demonstrates how to define a function that accepts a variable number of arguments.

main.jl
function sum_all(args...)
    return sum(args)
end

println(sum_all(1, 2, 3, 4))

The sum_all function sums all provided arguments. The triple dots (varargs syntax) collect the arguments into a tuple named args, which is then passed to sum. Calling sum_all() with no arguments raises an error, because sum cannot reduce an empty collection; write sum(args; init=0) if the empty case should return 0.

$ julia main.jl
10

Recursive Functions

This example shows how to define a recursive function.

main.jl
function factorial(n)
    return n == 0 ? 1 : n * factorial(n - 1)
end

println(factorial(6))

The factorial function calculates the factorial of a number recursively: it calls itself with a smaller argument until it reaches the base case n == 0. The ? and : form the ternary operator, a compact if/else expression.

$ julia main.jl
720

Higher-Order Functions

This example demonstrates how to pass functions as arguments.

main.jl
function apply(func, x)
    return func(x)
end

println(apply(x -> x^2, 9))

The apply function applies a given function to an argument. Functions are first-class values in Julia, so they can be passed to other functions just like numbers or strings. Here we pass an anonymous function that squares its input.

$ julia main.jl
81

Closures

This example shows how to create a closure in Julia.

main.jl
function make_counter()
    count = 0
    return () -> (count += 1)
end

counter = make_counter()
println(counter())
println(counter())
println(counter())

The make_counter function returns a closure that increments a counter each time it is called. The returned anonymous function captures the local variable count, which lives on after make_counter has finished. Each call changes that same captured value, so the printed numbers keep increasing.

$ julia main.jl
1
2
3

Best Practices for Functions

A few habits make functions easier to read and maintain.

Source

Julia Functions documentation

Julia Keyword Arguments documentation

In this article, we have explored various examples of using functions in Julia, including basic definitions, advanced features, and best practices. To group functions into namespaces, see Julia modules; for the building blocks used in their bodies, see Julia variables and Julia control flow.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Julia tutorials.