# Trick for optional arguments with defaults

**URL:** <https://fortran-lang.discourse.group/t/trick-for-optional-arguments-with-defaults/10852>\
**Category:** Uncategorized\
**Created:** [April 13, 2026, 11:15am UTC](https://fortran-lang.discourse.group/t/trick-for-optional-arguments-with-defaults/10852 "2026-04-13T11:15:11Z")\
**Posts on this page:** 1\
**Showing post:** 10

<div class="post-metadata">

**Author:** ![hkvzjal](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/hkvzjal/32/3055_2.png) [@hkvzjal](https://fortran-lang.discourse.group/u/hkvzjal)\
**Post date:** [April 13, 2026, 8:59pm UTC](https://fortran-lang.discourse.group/t/trick-for-optional-arguments-with-defaults/10852/10 "2026-04-13T20:59:03Z")

</div>

> [@PierU](#):
>
> A way to prevent such an error is to use an ASSOCIATE block that spans the whole routine:
> 
> ```auto
> subroutine bar(...,a)
> real, intent(in), optional :: a
> real :: a_local
> a_local = 2.0; if (present(a)) a_local = a
> ASSOCIATE( a => a_local )
> ... ! "a" can now be safely used as an alias to a_local
> END ASSOCIATE
> end subroutine
> 
> ```

Be wary though of possible performance penalties that can be unadvertely introduced due to the way some compilers interpret `associate` [Fortran skill markdown for codex - #22 by ivanpribec](https://fortran-lang.discourse.group/t/fortran-skill-markdown-for-codex/10783/22) it probably won’t be an issue for scalar optionals but it could if the optional is an array.

---

_[View the full topic](https://fortran-lang.discourse.group/t/trick-for-optional-arguments-with-defaults/10852)._
