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
Replay: real traced execution (multi-file project)
program keyword_arguments_demo
implicit none
integer :: subtotal, tax, total
subtotal = 20
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 = 15
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 = 30
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
subtotal ← 20
5subtotal = 206tax = add_tax(amount=subtotal, rate=2)values this step20subtotaltax ← 22
5subtotal = 206tax = add_tax(amount=subtotal, rate=2)7total = add_tax(rate=3, amount=subtotal)values this step22tax20subtotal2ratetotal ← 23
6tax = add_tax(amount=subtotal, rate=2)7total = add_tax(rate=3, amount=subtotal)8print '(I0, A, I0)', tax, " ", totalvalues this step23total20subtotal3rateprint '(I0, A, I0)', tax, " ", total
7 total = add_tax(rate=3, amount=subtotal)8 print '(I0, A, I0)', tax, " ", total9containsoutput22 23values this step22tax23total
subtotal ← 15
5subtotal = 156tax = add_tax(amount=subtotal, rate=2)values this step15subtotaltax ← 17
5subtotal = 156tax = add_tax(amount=subtotal, rate=2)7total = add_tax(rate=3, amount=subtotal)values this step17tax15subtotal2ratetotal ← 18
6tax = add_tax(amount=subtotal, rate=2)7total = add_tax(rate=3, amount=subtotal)8print '(I0, A, I0)', tax, " ", totalvalues this step18total15subtotal3rateprint '(I0, A, I0)', tax, " ", total
7 total = add_tax(rate=3, amount=subtotal)8 print '(I0, A, I0)', tax, " ", total9containsoutput17 18values this step17tax18total
subtotal ← 30
5subtotal = 306tax = add_tax(amount=subtotal, rate=2)values this step30subtotaltax ← 32
5subtotal = 306tax = add_tax(amount=subtotal, rate=2)7total = add_tax(rate=3, amount=subtotal)values this step32tax30subtotal2ratetotal ← 33
6tax = add_tax(amount=subtotal, rate=2)7total = add_tax(rate=3, amount=subtotal)8print '(I0, A, I0)', tax, " ", totalvalues this step33total30subtotal3rateprint '(I0, A, I0)', tax, " ", total
7 total = add_tax(rate=3, amount=subtotal)8 print '(I0, A, I0)', tax, " ", total9containsoutput32 33values this step32tax33total
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.