Delegates vs. Function Pointers, part 1

Most languages – with the unfortunate exception of Java – allow functions to be passed around as variables.  C has function pointers, .Net has delegates, and Javascript and most functional programming languages treat functions as first class objects.

There is a fundamental difference between C-style function pointers vs. delegates or function objects.  Pure function pointers cannot hold any state other than the function itself.  In contrast, delegates and function objects do store additional state that the function can use.

To illustrate this difference,  I will use a simple example.  Most programming environments have a filter function that takes a collection and a predicate (a function that takes an item from the collection and returns a boolean), and returns a new collection with those items from the original collection that match the predicate.  In C#, this is the LINQ Where method; in Javascript, it’s the Array.filter method (introduced by Javascript 1.6).

Given a list of numbers, you can use such a method to find all numbers in the list that are greater than a local variable x.  However, in order to do that, the predicate have access to x.  In subsequent posts, I’ll explore how this can be done in different languages.

13 comments:

Thank you! I have been looking for an explanation of the difference for a while and this helps a lot.

The information you share is very useful. It is closely related to my work and has helped me grow. Thank you!
bodak yellow lyrics ,
colour switch

This is amazing how you write clearly :) thanku for sharing
Raksha Bandhan Images Pics

and also see my this post check one this is amazing :)
Raksha Bandhan Shayari

The above article is nice and interesting, thank you willing to share! Greetings success of admin Percetakan Murah Rawamangun Jakarta Timur wish you deign to visit my website, thank you :)

By the way, this info will help you to write your perfect resume. It's important to know if you want to achieve success

BestWritingService.co.uk is a company with considerable experience providing quality essay writing services uk. If you want the purchase exceptional resume you should choose the best writing service co ul writing service.

It is important for medical science research paper service students to seek Medical Science Assignment Help from a reputable custom medical science writing company so as to be assured of good grades in their medical science essay writing services.

Finding the best Help with Medical Assignment is not easy unless one is keen to establish a professional medical assignment help & medical homework help online.

Engineering assignment writing services are essential for college & university engineering research paper services seekers and Engineering Writing Services.

Post a Comment