Emma Boudreau
1 min readJan 9, 2024

--

In Julia, `Methods` are part of a `Function`'s signature. You might be familiar with object-oriented programming.. In object-oriented programming, `Methods` are a part of a class, or part of a type to put this into Julia terminology.

A `Method` in Julia is simply a `Function` with the types of its arguments. Here is an example of a `Method`:

```

function example(x::Int64)

end

```

The `Function` for this `Method` is `example`, and the one existing `Method` for it is `example(::Int64)`, or example with an Int64 as the first positional argument. We can add another method simply by writing one.

```

function example(x::String)

end

```

We can introspect methods with the `methods` method:

julia> methods(example)

# 2 methods for generic function "example" from Main:

[1] example(x::Int64)

@ REPL[1]:1

[2] example(x::String)

@ REPL[2]:1

We can even define a function with no methods.

```

julia> function sample end

sample (generic function with 0 methods)

julia> methods(sample)

# 0 methods for generic function "sample" from Main

julia> function sample(x::Int64, y::Int64)

end

sample (generic function with 1 method)

julia> methods(sample)

# 1 method for generic function "sample" from Main:

[1] sample(x::Int64, y::Int64)

@ REPL[6]:1

```

Whenever the right argument types for any of these methods are provided to the function, it runs the appropriate method corresponding to those arguments.

--

--

Emma Boudreau
Emma Boudreau

Written by Emma Boudreau

i am a computer nerd. I love art, programming, and hiking. https://github.com/emmaccode

No responses yet