I like to think of a callback as a way to provide instance specific processing to an abstract algorithm. Say you want to be able to perform some processing on each element of an array. You could easily write a program to loop through all the elements of an array, and add 2 to each element of a numeric array. but what if you now need to do something else to that numeric array, say set all the elements to zero. Ok, that is trivial in RPG, but just stay with me in concept. You could write another program to loop through all the elements of the array, and set each to zero. But now you have two functions with a lot of duplicate code. One way to remove that duplication would be to write an "array walk" function that would loop through the array, and for each element call a function to process that element. A pointer to the "Add 2" function would need to be passed to the "array walk" function so that the "Array Walk" function could call the "Add 2" function. Now to do something different to each element of the array you just write a new function to "Set to Zero", or "Divide by 10", or whatever, and pass the appropriate function pointer to the "Array Walk" function. Those "Add 2", "Set to Zero", and "Divide by 10" functions are call backs. It allows a developer to write a generic function to perform some common function like iterate through an array while performing the same operation on each element. The array does not need to be defined to the generic function, just to the call back. All the array iterator needs to know how to do is to iterate through an array, and how to call the processing function provided by the caller.

Now the array iterator would probably not be used to perform tasks as trivial as clearing the array, or adding 2 to each element of a simple numeric array, but what if you had an array of order lines, and you needed to calculate sales tax on each line? You could use an array iterator, and all you wold have to do would be to write a sales tax calculation function that applied to a single order line. Now without having to deal with the vagaries of processing the order lines, you could simply apply the sales tax calculation to each line of the order by using the array iterator function with a callback to the sales tax calculation function. Say for certain orders you want to be able to apply a discount to all sales lines that exceed $50.00. You only have to write the function that checks a sales line for the threshold amount, and applies the discount. Then apply it to the array using the iterator. It gives you a simple way to separate business rules from control logic. It also gives you a decent way to avoid writing the same code over and over again.

Mark Murphy
STAR BASE Consulting, Inc.

-----midrange-l-bounces@xxxxxxxxxxxx wrote: -----
To: Midrange Systems Technical Discussion <midrange-l@xxxxxxxxxxxx>
From: Richard Reeve
Sent by: midrange-l-bounces@xxxxxxxxxxxx
Date: 10/15/2012 04:50PM
Subject: Callback

Has anyone ever heard of a callback as it is related to the IBM i?  I was asked to explain a callback during an interview and I'd never even heard of it.   Can any of you explain to me what a callback is and how it is used?  I tried google but didn't get a good explanation.

Warmest Regards,

Richard Reeve

This thread ...


Return to Archive home page | Return to MIDRANGE.COM home page