Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Variadic function
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== In Fortran === Since the Fortran 90 revision, [[Fortran]] functions or subroutines can accept optional arguments:<ref>{{Cite web |title=Optional Arguments |url=https://www.intel.com/content/www/us/en/docs/fortran-compiler/developer-guide-reference/2023-0/optional-arguments.html |access-date=2025-03-18 |website=Intel |language=en}}</ref> the argument list is still fixed, but the ones that have the {{code|optional}} attribute can be omitted in the function/subroutine call. The intrinsic function {{code|present()}} can be used to detect the presence of an optional argument. The optional arguments can appear anywhere in the argument list. <syntaxhighlight lang="fortran">program test implicit none real :: x !> all arguments are passed: call foo( 1, 2, 3.0, 4, x ) !< outputs 1 \ 2 \ 3.0 \ 4 \ 6.0 (the "\" denotes a newline) !> the last 2 arguments are omitted: call foo( 1, 2, 3.0 ) !< outputs 1 \ 2 \ 3.0 !> the 2nd and 4th arguments are omitted: the arguments that are positioned after !> an omitted argument must be passed with a keyword: call foo( 1, c=3.0, e=x ) !< outputs 1 \ 3.0 \ 6.0 !> alternatively, the Fortran 2023 revision has introduced the .NIL. pseudo constant !> to denote an omitted argument call foo( 1, .NIL., 3.0, .NIL., x ) !< outputs 1 \ 3.0 \ 6.0 contains !> the subroutine foo() has 2 mandatory and 3 optional arguments subroutine foo( a, b, c, d, e ) integer, intent(in) :: a integer, intent(in), optional :: b real, intent(in) :: c integer, intent(in), optional :: d real, intent(out), optional :: e print*, a if (present(b)) print*, b print*, c if (present(d)) print*, d if (present(e)) then e = 2*c print*, c end if end subroutine end program</syntaxhighlight> '''Output:''' <pre> The sum of [1 2] is 3 The sum of [1 2 3] is 6 The sum of [1 2 3 4] is 10 </pre>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)