Named parameters: Difference between revisions

fortran
(smalltalk)
(fortran)
Line 4:
* [[Varargs]]
* [[Optional parameters]]
 
=={{header|Fortran}}==
{{works with|Fortran|95 and later}}
 
Fortran accepts named parameter and optional parameter. Arguments are always named: if the name is omitted, the order used in the definition of the function / subroutine must be used.
 
<lang fortran> subroutine a_sub(arg1, arg2, arg3)
integer, intent(in) :: arg1, arg2
integer, intent(out), optional :: arg3
! ...
end subroutine a_sub</lang>
 
<lang fortran>! usage
integer :: r
call a_sub(1,2, r)
call a_sub(arg2=2, arg1=1)</lang>
 
The presence of an optional argument can be tested with <tt>PRESENT</tt>; if optional arguments come before a non optional argument, the usage of the name of the argument is mandatory.
 
<lang fortran> subroutine b_sub(arg1, arg2)
integer, intent(in), optional :: arg1
integer, intent(in) :: arg2
!...
end subroutine b_sub</lang>
 
<lang fortran> call b_sub(1) ! error: missing non optional arg2
call b_sub(arg2=1) ! ok
call b_sub(1, 2) ! ok: arg1 is 1, arg2 is 2
call b_sub(arg2=2, arg1=1) ! the same as the previous line</lang>
 
=={{header|Objective C}}==