# How do I define a structure inside a function?

**URL:** <https://discourse.julialang.org/t/how-do-i-define-a-structure-inside-a-function/132379>\
**Category:** New to Julia\
**Tags:** struct\
**Created:** [September 15, 2025, 6:50am UTC](https://discourse.julialang.org/t/how-do-i-define-a-structure-inside-a-function/132379 "2025-09-15T06:50:58Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![hack3rcon](https://avatars.discourse-cdn.com/v4/letter/h/96bed5/32.png) [@hack3rcon](https://discourse.julialang.org/u/hack3rcon)\
**Post date:** [September 15, 2025, 6:50am UTC](https://discourse.julialang.org/t/how-do-i-define-a-structure-inside-a-function/132379/1 "2025-09-15T06:50:58Z")

</div>

Hello,  
I want to define a structure inside a function:

```julia-auto
function PrintDate(d::Date)
    struct Date 
        day::Int64
        month::String
        year::Int64
    end
    println(d.day,d.month,d.year)
end

chmas = Date(25,"Dec",2025)

PrintDate(chmas)

```

But I got the following error:

```julia-auto
ERROR: LoadError: syntax: "struct" expression not at top level

```

How to solve it?

Thank you.

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [September 15, 2025, 6:54am UTC](https://discourse.julialang.org/t/how-do-i-define-a-structure-inside-a-function/132379/2 "2025-09-15T06:54:25Z")

</div>

I have never seen a structure defined inside a function. Is there a reason why you don’t want to define it outside?

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [September 15, 2025, 7:08am UTC](https://discourse.julialang.org/t/how-do-i-define-a-structure-inside-a-function/132379/3 "2025-09-15T07:08:11Z")

</div>

Why not define the structure outside of the function?

```julia-auto
julia> struct Date 
           day::Int64
           month::String
           year::Int64
       end

julia> function PrintDate(d::Date)
       println(d.day,d.month,d.year)
       end
PrintDate (generic function
 with 1 method)

julia> chmas = Date(25,"Dec",2025)
Date(25, "Dec", 2025)

julia> PrintDate(chmas)
25Dec2025

```

If you want to create structures on the fly, consider using named tuples

```julia-auto
julia> chmas = (day=25,month="Dec",year=2025)
(day = 25, month = "Dec", year = 2025)

julia> function PrintDate(d)
       println(d.day,d.month,d.year)
       end
PrintDate (generic function with 2 methods)

julia> PrintDate(chmas)
25Dec2025

```

---

<div class="post-metadata">

**Author:** ![hack3rcon](https://avatars.discourse-cdn.com/v4/letter/h/96bed5/32.png) [@hack3rcon](https://discourse.julialang.org/u/hack3rcon)\
**Post date:** [September 15, 2025, 7:12am UTC](https://discourse.julialang.org/t/how-do-i-define-a-structure-inside-a-function/132379/4 "2025-09-15T07:12:58Z")

</div>

Hello,  
Thank you for your answers.  
So, is this impossible?

---

<div class="post-metadata">

**Author:** ![sgaure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sgaure/32/14779_2.png) [@sgaure](https://discourse.julialang.org/u/sgaure)\
**Post date:** [September 15, 2025, 7:33am UTC](https://discourse.julialang.org/t/how-do-i-define-a-structure-inside-a-function/132379/5 "2025-09-15T07:33:24Z")

</div>

> [@hack3rcon](#):
>
> So, is this impossible?

Yes. It’s says so. “not at top level”. You can however run top level stuff inside a function with `eval` or `@eval`:

```julia-auto
function f()
    @eval struct Foo
        a::Int
    end
end

```

and even create it with you own name

```julia-auto
function g(name)
    @eval struct $(Symbol(name))
        a::Int
    end
end
g("MyStruct")

```

---

<div class="post-metadata">

**Author:** ![barucden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barucden/32/26154_2.png) [@barucden](https://discourse.julialang.org/u/barucden)\
**Post date:** [September 15, 2025, 9:02am UTC](https://discourse.julialang.org/t/how-do-i-define-a-structure-inside-a-function/132379/6 "2025-09-15T09:02:00Z")

</div>

The question sounds like an [XY problem](https://xyproblem.info/).

I suspect that you are trying to solve a specific problem, for which defining a structure in a function seemed like a good solution. If you share the original problem here, you are likely going to get much better suggestions.

---

<div class="post-metadata">

**Author:** ![hack3rcon](https://avatars.discourse-cdn.com/v4/letter/h/96bed5/32.png) [@hack3rcon](https://discourse.julialang.org/u/hack3rcon)\
**Post date:** [September 15, 2025, 9:17am UTC](https://discourse.julialang.org/t/how-do-i-define-a-structure-inside-a-function/132379/7 "2025-09-15T09:17:52Z")

</div>

Hello,  
I’m learning Julia and here I’ll ask questions that come to my mind.

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [September 15, 2025, 9:42am UTC](https://discourse.julialang.org/t/how-do-i-define-a-structure-inside-a-function/132379/8 "2025-09-15T09:42:25Z")

</div>

Of course any questions are welcome. What @barucden meant to say is that, from the perspective of people who have been using Julia for a while, if you feel like you need to define a struct inside a function, you may need to rethink your approach.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [September 15, 2025, 9:47pm UTC](https://discourse.julialang.org/t/how-do-i-define-a-structure-inside-a-function/132379/9 "2025-09-15T21:47:33Z")

</div>

In particular, most use cases for a struct defined in a function are covered either by named tuples, as mentioned, or parametric types.
