Friday, March 31, 2017
Passing Functions To Functions In C C Java
Passing Functions To Functions In C C Java
Before I start the article, I admit I used the word "Function" a little loosely in the title. Functions are available in C++, but in C# and Java it would be more appropriate to call them methods of a class because of the Object Oriented approach followed by these languages.
There are a lot of situations where we write modules such that other developers have the flexibility to fine-tune them to meet their requirements. Assume we are writing a module (function/method) wherein a function/method will be called in the middle of its execution but we dont know the implementation of this function/method nor its actual name. Wouldnt it nice if we could just leave the implementation of the function/method to the developer who will be using the module?
This is exactly what I target in this blog-post. The article will give a quick overview of how to pass a function (actually a reference to a function) to another function in C++; how to create and use delegates in C#; and finally how to simulate a similar effect in Java which has neither pointers nor delegates.
To help the readers understand the concept better, I will be taking a simple example here. In the example, there is an addition module which takes three arguments - two integers and a function/method which is the display routine. Its not a real-world example as I could have just returned the sum back.
An apt real-world example for this approach would a customized sort routine which takes a function/method which can be used as a comparator for the sort. I will leave it to the users to think of more complex situations where such an approach will be beneficial.
Function Pointers in C++
A Function Pointer is a pointer in C/C++ which points to a function. When dereferenced, a function pointer will result in the execution of the function it points to. A function pointer is defined as -
return_type (*function_name)(argument_list)
An important point that is to be noticed here is that the signature of the function to which the pointer can point to has to be declared in the declaration of the pointer. Once that is done, it can be assigned to any function which follows that signature.
Here is an example C++ program which uses function pointers -

Function pointers play a very important in Callback layered architectures. For example, function pointers can be used to register a function with an event handler.
Delegates in C#
A delegate is a type that references to a method. When a method is assigned to a delegate, the delegate behaves exactly like the method. A delegate in C# is defined as -
access_modfier delegate return_type delegate_name(argument_list)
Similar to Function Pointers in C/C++, the method signature has to be specified in the definition of the delegate. But unlike function pointers, delegates are type safe. This is because since Function Pointers are basically pointers, even an improper assignment will not raise any error until its too late. On the other hand delegates are associated with the signature specified and any wrong assignment will lead to a compilation error.
Another important feature of delegates is that delegates can be chained together. That is, multiple functions can be executed when the delegate is called. This is done through + and - operations on delegates. Here is how the syntax looks -
delegate_name reference_name;
reference_name = method1_name;
reference_name += method2_name;
Now when reference_name is called, both method1_name and method2_name are executed one after another.
Delegates also allow methods to be passed to other methods using lambda expressions, which can be written in the method call itself. Go through the following example which shows how to use delegates for method passing and how lambda expressions are useful -

Delegates are very useful in event handling, for defining callback methods like a customized sort, etc.
Simulation Using Interfaces in Java
Ok, now coming to the fun part. How can I get the same effect in Java which has neither Function Pointers nor Delegates?
The approach which have discussed above can be simulated in Java using single method Interfaces. The single method interface can be implemented and the class can become an argument of the method. The concrete class passed to the method is used to call the one and only method of the interface.
However, there are three major drawbacks in this -
- Every developer using the method is forced to create a class implementing the interface
- The name of the function is fixed, the implementation will change but not its name
- We are not passing the method anymore but instead passing an object which has the required method
Similar to C#, the interface implementation can be done in-line to the method call using anonymous classes.
Here is a Java program which achieves the same result as the above two examples -

As we know, C# was heavily influenced by Java. So, almost everything that we can do in Java can be done in C#. The interface technique discussed for Java can also be applied in C#, but I guess its more easy to use Delegates for such a requirement than creating Interfaces.
Available link for download
Monday, February 6, 2017
P A C H A M A M A
P A C H A M A M A
Earlier this week we tried something a little bit different...Something...South American
A short walk from Selfridges, down on Thayer street you will find a doorway to a beautiful, airy, Capricious Peruvian-British restaurant named Pachamama.
{If you want to see our trip in video form vlogged it here}

We went for a spot of brunch as its a beautifuly lit, tucked away little mid-day food spot.It was one of the hardest menus to choose from! There were some foods which I had never heard of but the accommodating waiters are quick to explain everything to us and give their personal recommendations.
They do small plates and bigger plates - Perfectly designed to share around your group or if you are a bit of a Joey like us, then a small plate and a large plate to yourself will work just fine.

I went for the Salmon, beetroot & Avocado Ceviche & The Smoked Brisket, Plantain Hash, aji HP
Jason went for the Pan Con Chicharron (Voted Londons best burger) & Prawn, squid, Quinoa Chaufa (Chaufa: A Peruvian fried rice dish)

I went for the Salmon, beetroot & Avocado Ceviche & The Smoked Brisket, Plantain Hash, aji HP
Jason went for the Pan Con Chicharron (Voted Londons best burger) & Prawn, squid, Quinoa Chaufa (Chaufa: A Peruvian fried rice dish)

Now I know I get excited about things very often, but that doesnt mean that Im overselling anything or blowing things out of proportion - The Smoked brisket was one of the TASTIEST meals I have had in London, It was extremely soft, the flavour was so rich and tangy, what topped it off was the Plantain.
Growing up with a Guyanese family/grandmother, I have such fond memories of her cooking and the smells that would emanate from the kitchen. This juicy plantain brought it all back. (Its hidden under the sauce in the picture.)

Jason gave very good reports from over from his burger, he says it was delicious, the only thing wrong was that there was Coriander inside and this reminds me, I had to pick out some coriander from the dishes too as it is one of the only tastes I despise!
So if youre like us then just be sure to ask for it to be prepared without Coriander as most of it was only on the top or easy to take out anyway.
I tried some of Jasons Quinoa Chacha too and it was extremely tasty, such a healthy yet totally satisfying lunch dish.
I also have to give a little shoutout to the Regents Park Rum-based cocktail, divine!
I am itching to go back to Pachamama, The place is so exciting for the senses, even just to look at. It is such a beautiful location with a main room but also little rooms tucked away for more private meals or drinks.
Thank you for having us!
Sammi x
Available link for download