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.

14 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.

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.

If you are facing problem regarding HP Officejet Printer Offline. wps pin for hp printer · How to Resolve HP DESKJET PRINTER OFFLINE SUPPORT.

myassignmenthelp is the best assignment service provider and high-quality content with the help of our professional writer. Our services are essays, assignments writing, research reports.

I am really happy to see your blog. This is informative and interesting article. It's related to languages like java, C++. Keep sharing your content. Now it's time to get services shutters in birmingham for more information.

Post a Comment