Passing a function as an argument

Hi

I understand that you can pass a function as an argument by setting it up like this:

var foo = function() { print("HELLO!"); };

// define your outer function normally
function boo(passedFunction) {
  passedFunction();
}

boo(foo);

and that you can add an argument to the inner function:

var foo = function( anInt : int ) { print("The number is: " + anInt); };

// define your outer function normally
function boo(passedFunction, anInt : int) {
  passedFunction(anInt);
}

boo(foo, 5);

Is this the most sensible way to approach it, or is there a way I could set it up to be:

boo(foo(5));

etc, which looks more similar to the standard function formatting in the brackets, or would you say thats just not needed? Cheers

The way you set it up is how to do it.

OK thanks

Hi,

That’s working in JS, right?
But in C#, I didn’t get it to work.
What identifier I have to choose for “passedFunction”??

With best regards,
Unity Nerd

using UnityEngine;
using System.Collections;

public delegate void ExampleDelegate (string parameter);


public class MyBehaviour : MonoBehaviour
{
	void Start ()
	{
		SomeHandler (MyTarget);
	}
	
	
	void SomeHandler (ExampleDelegate theDelegate)
	{
		theDelegate ("Hello, delegate!");
	}
	
	
	void MyTarget (string parameter)
	{
		Debug.Log (parameter);
	}
}
1 Like

Thank you very much AngryAnt!!!
Great example, works perfect for me!

With best regards,
UnityNerd

take off pragma strict if type error

var foo = function() :int {return 123};//print was no int, is nothing

 

// define your outer function normally

function boo(passedFunction):int {

  passedFunction();

}
or
var boo :Function = foo;
 

boo();

here is the best and simple way to pass function as an argument using C#

void Start ()
    {
        method1(method2);
    }

    void method1(System.Action getMethod)
    {
        getMethod();
    }

    void method2()
    {
        print("called");
    }
1 Like

If you prefer to not define your own delegates, there are indeed built-in ones you can use. For completions sake, others include:

using System;

void CallParameterised (Action<string> theReference)
{
    theReference ("The string");
}


bool CallWithReturn (Func<bool> theReference)
{
    return theReference ();
}


bool CallParameterisedWithReturn (Func<string, bool> theReference)
{
    return theReference ("The parameter");
}

Note that you can add a bunch of parameters to both of them - the generic argument list for Func just has to end with the return type.

3 Likes

Thanks for the info @AngryAnt .

Followup question: if I have this code:

public static decimal[] GetRandomEquivalentFraction(Func<int, List<int>> numeratorMethod, List<int> denominators, decimal[] operands)

How would I use a delagate instead of a function call as a parameter? The method numeratorMethod takes in an int as a parameter, and passes back a list of ints. I cannot seem to get the syntax correct in order to replace it with System.Action.

numeratorMethod looks like this:

public List<int> Numerators(int denominator)
{
    return FractionUtils.ParseNumerators(numerators, denominator);
}

And the call within GetRandomEquivalentFraction to numeratorMethod looks like this:

List<int> numerators = numeratorMethod(denominator);