# How to prohibit assignment to a variable in a function?

**URL:** <https://discourse.julialang.org/t/how-to-prohibit-assignment-to-a-variable-in-a-function/49544>\
**Category:** General Usage\
**Created:** [November 3, 2020, 6:13pm UTC](https://discourse.julialang.org/t/how-to-prohibit-assignment-to-a-variable-in-a-function/49544 "2020-11-03T18:13:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Qiyamah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/qiyamah/32/2660_2.png) [@Qiyamah](https://discourse.julialang.org/u/Qiyamah)\
**Post date:** [November 3, 2020, 6:13pm UTC](https://discourse.julialang.org/t/how-to-prohibit-assignment-to-a-variable-in-a-function/49544/1 "2020-11-03T18:13:51Z")

</div>

```julia
function f(x::Int)
     const y = 5
     #some code...
     y =2 # I want this line to throw "ERROR: invalid redefinition of constant y"
     x+y
end

```

Instead it throws:  
ERROR: syntax: unsupported `const` declaration on local variable around REPL[37]:2

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [November 3, 2020, 6:18pm UTC](https://discourse.julialang.org/t/how-to-prohibit-assignment-to-a-variable-in-a-function/49544/2 "2020-11-03T18:18:56Z")

</div>

Short answer you can’t.  
Long answer: you can’t but you shouldn’t need to.  
Make your function short enough and clear enough that the logic is correct and clearly correct and it doesn’t mater.

I do think this would be a nice feature to have, or even a macro that i could annotate a function with to say that it won’t ever reassign anything.  
Just because I hate it when variables are reassigned.  
Probably makinga macro to detect htat is not impossible

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [November 3, 2020, 6:57pm UTC](https://discourse.julialang.org/t/how-to-prohibit-assignment-to-a-variable-in-a-function/49544/3 "2020-11-03T18:57:28Z")

</div>

You can always defined a named tuple in the local scope with all “constant variables” and give it a short name like `c`. But you probably will lose some time with errors just because you forgot to put `c.` before the name of a variable.
