Passing Function as an argument to another Function

Vino vino.bheeman at hotmail.com
Mon Dec 4 10:36:31 UTC 2017


On Monday, 4 December 2017 at 08:27:10 UTC, rikki cattermole 
wrote:
> On 04/12/2017 8:22 AM, Vino wrote:
>> Hi All,
>> 
>>    Request your help on the below code, I want to send the 
>> name of the function ( First and Second) from main as an 
>> argument to another function(Mid) and the function "Mid" has 
>> to execute the function(First and Second).
>> 
>> Program:
>> import std.stdio;
>> 
>> void First (string Ftext) {
>> writeln("First :", Ftext);
>> }
>> 
>> void Second (string Stext) {
>> writeln("Second :", Stext);
>> }
>> 
>> void Mid( string Fun, string Mtext) {
>> Fun(Mtext);
>> }
>> 
>> void main () {
>> string Ftext = "FTest1";
>> string Stext = "STest2";
>> Mid(First, Mtext);
>> Mid(Second, Stext);
>> }
>> 
>> From,
>> Vino.B
>
> Not going to happen like that.
>
> void mid(void function(string) func, string mtext) {
> 	func(mtext);
> }
>
> void main() {
> 	string ftext = "ftest1",
> 		stext = "stest2";
> 	mid(&first, ftext);
> 	mid(&second, stext);
> }

Hi Rikki,

   Thank you very much, I tired the above code and it is working, 
so another help on the same topic.
IF my function (First) of below type when who do we define the 
Mid function

Array!(Tuple!(string, string))  First(string Ftext)

Tried the below  but no luck
void mid(Array!(Tuple!(string, string)) function(string) func, 
string mtext) {
  	func(mtext);
}


From,
Vino.B



More information about the Digitalmars-d-learn mailing list