Skip to content

Instantly share code, notes, and snippets.

@dorchard
Last active June 1, 2023 18:06
Show Gist options
  • Save dorchard/3cc13fe75d6d109cb75ec11d41ddc104 to your computer and use it in GitHub Desktop.
Save dorchard/3cc13fe75d6d109cb75ec11d41ddc104 to your computer and use it in GitHub Desktop.
Fortran, function overloading on arity
program example
use naryfunc
implicit none
! Outputs 2 6 24
write(*,*) mult(2), mult(2, 3), mult(2, 3, 4)
end program example
module naryfunc
implicit none
private
public mult
interface mult
module procedure mult1, mult2, mult3
end interface
contains
pure function mult1(x)
integer, intent(in) :: x
integer :: mult1
mult1 = x
end function mult1
pure function mult2(x, y)
integer, intent(in) :: x, y
integer :: mult2
mult2 = x * y
end function mult2
pure function mult3(x, y, z)
integer, intent(in) :: x, y, z
integer :: mult3
mult3 = x * y * z
end function mult3
end module naryfunc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment