Interfaces and Optional Arguments
Keyword Arguments
Calling by Name
Fortran calls can name arguments explicitly. Keyword calls make the mapping clear and can appear in a different order.
Program
Play the program to call the same function with two keyword argument orders.
keyword_arguments.f90
program keyword_arguments_demo
implicit none
integer :: subtotal, tax, total
subtotal =
tax = add_tax(amount=subtotal, rate=2)
total = add_tax(rate=3, amount=subtotal)
print '(I0, A, I0)', tax, " ", total
contains
function add_tax(amount, rate) result(out)
integer, intent(in) :: amount, rate
integer :: out
out = amount + rate
end function add_tax
end program keyword_arguments_demo
program keyword_arguments_demo
implicit none
integer :: subtotal, tax, total
subtotal =
tax = add_tax(amount=subtotal, rate=2)
total = add_tax(rate=3, amount=subtotal)
print '(I0, A, I0)', tax, " ", total
contains
function add_tax(amount, rate) result(out)
integer, intent(in) :: amount, rate
integer :: out
out = amount + rate
end function add_tax
end program keyword_arguments_demo
program keyword_arguments_demo
implicit none
integer :: subtotal, tax, total
subtotal =
tax = add_tax(amount=subtotal, rate=2)
total = add_tax(rate=3, amount=subtotal)
print '(I0, A, I0)', tax, " ", total
contains
function add_tax(amount, rate) result(out)
integer, intent(in) :: amount, rate
integer :: out
out = amount + rate
end function add_tax
end program keyword_arguments_demo
keyword argument
`amount=subtotal` binds by dummy-argument name.
order independence
Keyword arguments can be written in a different order.
explicit interface
Contained procedures have an explicit interface, enabling keyword calls.