# How do optional variables work?

**URL:** <https://fortran-lang.discourse.group/t/how-do-optional-variables-work/7129>\
**Category:** Uncategorized\
**Created:** [January 7, 2024, 6:27pm UTC](https://fortran-lang.discourse.group/t/how-do-optional-variables-work/7129 "2024-01-07T18:27:02Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![Beliavsky](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Beliavsky](https://fortran-lang.discourse.group/u/Beliavsky)\
**Post date:** [January 7, 2024, 6:48pm UTC](https://fortran-lang.discourse.group/t/how-do-optional-variables-work/7129/3 "2024-01-07T18:48:27Z")

</div>

Since [An unallocated variable passed as an argument is not PRESENT](https://fortran-lang.discourse.group/t/an-unallocated-variable-passed-as-an-argument-is-not-present/1724), your `something_recognized_as_not_present` could be an allocatable scalar that has not been allocated, as in the following code

```auto
program hello
implicit none
integer :: a
integer, allocatable :: b
a = 5
call print_hello() ! prints "variable not present"
call print_hello(a) ! prints "variable is 5"
call print_hello(b) ! prints "variable not present"
allocate (b, source=3)
call print_hello(b) ! prints "variable is 3"
contains
subroutine print_hello(some_variable)
implicit none
integer, optional :: some_variable
if (present(some_variable)) then
   print*,"variable is", some_variable
else
   print*,"variable not present"
end if
end subroutine print_hello
end program hello

```

Quoting [fortran: Detecting null-pointer passed as dummy argument - Stack Overflow](https://stackoverflow.com/questions/74625857/fortran-detecting-null-pointer-passed-as-dummy-argument), the reason your code in the 2nd post works is that

> A nonpointer, nonallocatable, optional dummy argument will be treated as not present if associated with a disassociated pointer actual argument.

---

_[View the full topic](https://fortran-lang.discourse.group/t/how-do-optional-variables-work/7129)._
