# High order functions in Pascal

**URL:** <https://forum.castle-engine.io/t/high-order-functions-in-pascal/969>\
**Category:** Uncategorized\
**Created:** [November 4, 2023, 4:36pm UTC](https://forum.castle-engine.io/t/high-order-functions-in-pascal/969 "2023-11-04T16:36:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Timur](https://forum.castle-engine.io/letter_avatar_proxy/v4/letter/t/c67d28/32.png) [@Timur](https://forum.castle-engine.io/u/Timur)\
**Post date:** [November 4, 2023, 4:36pm UTC](https://forum.castle-engine.io/t/high-order-functions-in-pascal/969/1 "2023-11-04T16:36:23Z")

</div>

Hello. I am Timur. I am machine (hydraulic presses) repairer with some programming skils (basic c++ for the moment).

Does Pascal support higher-order functions? Let’s say I want to change the behavior of a character. in c++, I can pass either a pointer to a function or a lambda or std::function inside a class and thus I pass a new behavior program. I do not know if there is a similar thing in the Pascal language.

There is example on cpp (the one language I more-less know for the moment, but pascal looks interesting).

#include   
#include   
#include

struct fine\_grinder  
{  
void run(std::string coffee\_beans)  
{  
std::cout \<\< "cool! fine grinder works with " \<\< coffee\_beans \<\< std::endl;  
}  
};

struct bad\_grinder  
{  
void run(std::string coffee\_beans)  
{  
std::cout \<\< "baaad grinder works with " \<\< coffee\_beans \<\< std::endl;  
}  
};

// templated function that will use any grinder  
template void grinder\_func(std::string coffee\_beans)  
{  
Grinder\_type grinder;  
grinder.run(coffee\_beans);  
}

struct Cofemachine  
{

```
void grind_coffee(std::string coffee_beans)
{
    grinder_(coffee_beans);
}

void set_grinder(std::reference_wrapper<void(std::string)> grinder)
{
    grinder_ = grinder;
}

std::reference_wrapper<void(std::string)> grinder_ = grinder_func<bad_grinder>;

```

};

int main()  
{

```
Cofemachine mycoffemaker;
mycoffemaker.grind_coffee(std::string{"coffee_beans coffee"});

// here we pass into mycoffemaker the grinder_function filled with fine_grinder object
mycoffemaker.set_grinder(grinder_func<fine_grinder>);

mycoffemaker.grind_coffee(std::string{"coffee_beans coffee"});

return EXIT_SUCCESS;

```

}

---

<div class="post-metadata">

**Author:** ![michalis](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/michalis/32/3_2.png) [@michalis](https://forum.castle-engine.io/u/michalis)\
**Post date:** [November 6, 2023, 3:57pm UTC](https://forum.castle-engine.io/t/high-order-functions-in-pascal/969/2 "2023-11-06T15:57:21Z")

</div>

Hello! And yes 🙂

1. The base concept to pass an address of a function (global routine, or a method of some class) is documented as _“Callbacks (aka events, aka pointers to functions, aka procedural variables)”_ on [Modern Object Pascal Introduction for Programmers | Castle Game Engine](https://castle-engine.io/modern_pascal#Callbacks) .

2. To go one step further, if you’re looking specifically for lambdas / anonymous functions, that is also supported: [Anonymous Methods in Delphi - RAD Studio](https://docwiki.embarcadero.com/RADStudio/Sydney/en/Anonymous_Methods_in_Delphi) . This is a link to Delphi docs, which supports them out-of-the-box.

3. Finally, your example in C++ adds a twist to this: The function you pass around is a template.
