Higher-order functions: Difference between revisions

another way for fortran
m (→‎{{header|JavaScript}}: Removed semi-colons from last two examples' anonymous functions)
(another way for fortran)
Line 379:
{{works with|Fortran|90 and later}}
use the EXTERNAL attribute to show the dummy argument is another function rather than a data object. i.e.
<lang fortran> FUNCTION FUNC3(FUNC1, FUNC2, x, y)
REAL, EXTERNAL :: FUNC1, FUNC2
REAL :: FUNC3
Line 385:
FUNC3 = FUNC1(x) * FUNC2(y)
END FUNCTION FUNC3 </lang>
 
Another way is to put the functions you want to pass in a module:
 
<lang fortran>module FuncContainer
implicit none
contains
 
function func1(x)
real :: func1
real, intent(in) :: x
 
func1 = x**2.0
end function func1
 
function func2(x)
real :: func2
real, intent(in) :: x
 
func2 = x**2.05
end function func2
 
end module FuncContainer
 
program FuncArg
use FuncContainer
implicit none
 
print *, "Func1"
call asubroutine(func1)
 
print *, "Func2"
call asubroutine(func2)
 
contains
 
subroutine asubroutine(f)
! the following interface is redundant: can be omitted
interface
function f(x)
real, intent(in) :: x
real :: f
end function f
end interface
real :: px
 
px = 0.0
do while( px < 10.0 )
print *, px, f(px)
px = px + 1.0
end do
end subroutine asubroutine
 
end program FuncArg</lang>
 
=={{header|Haskell}}==