# Implementation of a parametrized objective function without using module variables or internal subroutines

**URL:** <https://fortran-lang.discourse.group/t/implementation-of-a-parametrized-objective-function-without-using-module-variables-or-internal-subroutines/9919>\
**Category:** Uncategorized\
**Created:** [July 13, 2025, 3:28am UTC](https://fortran-lang.discourse.group/t/implementation-of-a-parametrized-objective-function-without-using-module-variables-or-internal-subroutines/9919 "2025-07-13T03:28:14Z")\
**Posts on this page:** 1\
**Showing post:** 31

<div class="post-metadata">

**Author:** ![ivanpribec](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/ivanpribec/32/3290_2.png) [@ivanpribec](https://fortran-lang.discourse.group/u/ivanpribec)\
**Post date:** [July 14, 2025, 3:24pm UTC](https://fortran-lang.discourse.group/t/implementation-of-a-parametrized-objective-function-without-using-module-variables-or-internal-subroutines/9919/31 "2025-07-14T15:24:00Z")

</div>

> [@zaikunzhang](#):
>
> In modern/popular languages such as C++, Python, Julia, and MATLAB, this can be implemented easily using closures, lambda expressions, anonymous functions, …

There are some fundamental problems here for languages like C and Fortran, more details here: [Funarg problem - Wikipedia](https://en.wikipedia.org/wiki/Funarg_problem).

With C++ the situation is a bit different; lambdas and `std::function` work well when used within the confines of C++, but as @certik has just alluded, it faces the same limitations when trying to pass code and data across language boundaries.

For example a lambda function can be converted to a C compatible function pointer, [only under certain restrictions](https://stackoverflow.com/a/28746827/4283055), namely that it doesn’t capture any variables from the enclosing scope.

As @themos has written [before,](https://fortran-lang.discourse.group/t/is-it-possible-to-define-the-product-of-2-procedure-pointers-at-runtime/7450/13)

> Personal take: The essence of Fortran is that all executable code is present at program start. Fortran will not compose executable code at runtime, and if it ever does it shouldn’t be called Fortran anymore. In Fortran, data comes and goes but code is written in stone.

To tie back to what @certik said,

> [@certik](#):
>
> How would you do this in C? Isn’t this exactly the same problem as in Fortran? For this reason most 3rd party libraries allow you to pass user data. If they don’t, that’s a deficiency of the 3rd party library I think.

To achieve the parameterized function while preserving reentrancy, the `solver` would need to “open” a way to pass data (in C this would be the `void *` omni-parameter).

---

_[View the full topic](https://fortran-lang.discourse.group/t/implementation-of-a-parametrized-objective-function-without-using-module-variables-or-internal-subroutines/9919)._
