-3

Is there a way of passing a function to another function and then executing it?

functionCaller(functionToBeCalled());
Device
  • 574
  • 4
  • 19

2 Answers2

1

In java 8 you can use a method reference or lambda

functionCaller(this::functionToBeCalled);

or

functionCaller(() -> functionToBeCalled());
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
1

I don't know if I understand very well your question, but effectively you can call a function in param of another function.

You can do this (I suppose your current language is Java):

// if write(...) and getValue() are static method of Writer class  
Writer.write(getValue());

// if write(...) and getValue() can just be used by instanciate an object
Writer writer = new Writer();
String val = writer.getValue();
writer.write(val);

There are basic Java programming lesson. Thanks

thepaulo
  • 370
  • 4
  • 10