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.

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.

In the context of cheap assignment writing service, delegates can be used to implement a callback mechanism. For example, a client could pass a delegate to an assignment writing service, and the service would call the delegate when the assignment is complete. This would allow the client to be notified when the assignment is ready, without having to poll the service for updates.

Delegates are objects that encapsulate a function pointer. They are more flexible than function pointers, as they can be assigned to different functions at runtime. Delegates are also contoura procedure orlando florida to use with events, as they can be used to connect and disconnect event handlers.

Post a Comment