Hello,

I want to optimize a function of several variables with respect to one particular variable.

What is a good practice to implement function currying, i.e. given f(x,y), define g(y) = f(x=\bar x)(y) with some fixed \bar x?

From recent related topic, one can use module variables, but do I understand correctly

that this is not a threadsafe solution? Can this be done with pure procedures?

I’m not very experienced in Fortran, can I write a function that returns another function?

```
! gfortran -o currying currying.f90
module currying
use, intrinsic :: iso_fortran_env, only : rk => real64
real(rk), protected :: factor
contains
pure real(rk) function mult(x,y)
real(rk), intent(in) :: x
real(rk), intent(in) :: y
mult = x*y
end function mult
subroutine init_currying(factor_value)
real(rk), intent(in) :: factor_value
factor = factor_value
end subroutine init_currying
pure real(rk) function mult_currying(x)
real(rk), intent(in) :: x
mult_currying = mult(factor,x)
end function mult_currying
end module currying
program test
use currying
write(*, *) mult(2.0_rk,2.0_rk)
call init_currying(2.0_rk)
write(*, *) mult_currying(2.0_rk)
write(*, *)
write(*, *) mult(3.0_rk,2.0_rk)
call init_currying(3.0_rk)
write(*, *) mult_currying(2.0_rk)
write(*, *)
end program test
! ./currying
! 4.0000000000000000
! 4.0000000000000000
! 6.0000000000000000
! 6.0000000000000000
```