# Function or struct？

**URL:** <https://discourse.julialang.org/t/function-or-struct/108376>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [January 5, 2024, 9:32am UTC](https://discourse.julialang.org/t/function-or-struct/108376 "2024-01-05T09:32:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jiang\_ming\_zhang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jiang_ming_zhang/32/204063_2.png) [@jiang\_ming\_zhang](https://discourse.julialang.org/u/jiang_ming_zhang)\
**Post date:** [January 5, 2024, 9:32am UTC](https://discourse.julialang.org/t/function-or-struct/108376/1 "2024-01-05T09:32:38Z")

</div>

```julia
struct MyType{T} 
    label::T
    x::Float64 
    y::Float64  
end
function MyType(; label = 1000, x = 0, y = 0)
    return MyType(label, x, y)
end

```

I am a bit confused. Here we have a function and a struct, both of which are named ‘MyType’. They both are associated with the () bracekts. How can I tell which is invoked? For example, in the line

```julia
return MyType(label, x, y)

```

It should be invoking the struct, right?

In this specific case, we can differentiate the two by the ; symbol, but what if in the definition of the function, the ; is omitted?

---

<div class="post-metadata">

**Author:** ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)\
**Post date:** [January 5, 2024, 9:39am UTC](https://discourse.julialang.org/t/function-or-struct/108376/2 "2024-01-05T09:39:20Z")

</div>

The struct implicitly defines a constructor function which just takes a positional argument for each field. You now defined another method for construction. This simply multiple dispatch. I encourage you to read more on [constructors in the manual](https://docs.julialang.org/en/v1/manual/constructors/).

I also want to point out that in many programming languages you call the type/class to construct an instance (cf. Python) so Julia isn’t special in that regard. The new thing is that multiple dispatch allows you to simply add new nethods to the constructor.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [January 5, 2024, 12:00pm UTC](https://discourse.julialang.org/t/function-or-struct/108376/3 "2024-01-05T12:00:14Z")

</div>

> [@jiang\_ming\_zhang](#):
>
> In this specific case, we can differentiate the two by the ; symbol, but what if in the definition of the function, the ; is omitted?

The `;` symbol just separates positional arguments from keyword arguments (there’s no such thing as arguments that can be passed by either position or keyword). The values assigned to the keywords aren’t what make them keywords, those are just default values, though it’s more common for keywords to have default values.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 5, 2024, 12:45pm UTC](https://discourse.julialang.org/t/function-or-struct/108376/4 "2024-01-05T12:45:58Z")

</div>

> [@jiang\_ming\_zhang](#):
>
> Here we have a function and a struct, both of which are named ‘MyType’. They both are associated with the () bracekts. How can I tell which is invoked?

Just to add a bit of clarification to the other excellent answers: technically, the `struct` is never “invoked”, its [inner constructor](https://docs.julialang.org/en/v1/manual/constructors/#man-inner-constructor-methods) may be invoked. People usually do not distinguish the two since `struct`s define an inner constructor so they can be used interchangeably, but nevertheless they are different concepts.

You can list them all constructors (inner and outer) with `methods(MyType)`. The one matching the [method signature](https://docs.julialang.org/en/v1/manual/methods/) will be invoked.
