# Questions about function inside struct

**URL:** <https://discourse.julialang.org/t/questions-about-function-inside-struct/129669>\
**Category:** New to Julia\
**Tags:** struct\
**Created:** [June 5, 2025, 12:57pm UTC](https://discourse.julialang.org/t/questions-about-function-inside-struct/129669 "2025-06-05T12:57:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cyan](https://avatars.discourse-cdn.com/v4/letter/c/278dde/32.png) [@Cyan](https://discourse.julialang.org/u/Cyan)\
**Post date:** [June 5, 2025, 12:57pm UTC](https://discourse.julialang.org/t/questions-about-function-inside-struct/129669/1 "2025-06-05T12:57:52Z")

</div>

Hi, all. I am new to Julia, is it possible to build a function in struct, and whether it is efficient in coding comparing with an independent function?

```julia
struct s
       a
       function f(x,a)
             return a*x
       end
end

```

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [June 5, 2025, 1:05pm UTC](https://discourse.julialang.org/t/questions-about-function-inside-struct/129669/2 "2025-06-05T13:05:26Z")

</div>

Sure, it’s valid to put almost any code in the body of `struct`. The body seems to act as if it was within a `let`, that is, it’s a local scope. So whatever you define in the body will not be available outside by default. It’s possible to override this using a `global` declaration.

---

<div class="post-metadata">

**Author:** ![Cyan](https://avatars.discourse-cdn.com/v4/letter/c/278dde/32.png) [@Cyan](https://discourse.julialang.org/u/Cyan)\
**Post date:** [June 5, 2025, 1:08pm UTC](https://discourse.julialang.org/t/questions-about-function-inside-struct/129669/3 "2025-06-05T13:08:30Z")

</div>

Thanks, may I ask which one is better? (function in struct or outside struct)

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [June 5, 2025, 1:27pm UTC](https://discourse.julialang.org/t/questions-about-function-inside-struct/129669/4 "2025-06-05T13:27:55Z")

</div>

> [@Cyan](#):
>
> Hi, all. I am new to Julia, is it possible to build a function in struct, and whether it is efficient in coding comparing with an independent function?

See: [Building function (like OOP) inside a struct - #2 by stevengj](https://discourse.julialang.org/t/building-function-like-oop-inside-a-struct/126180/2)
