Functions :: Functions as Parameters

Functions :: Functions as Parameters

  • This is a little more advanced section on functions, but is very useful. Take this for example:
      int applyeqn(int F(int), int max, int min) {
    int itmp;

    itmp = F(int) + min;
    itmp = itmp - max;
    return itmp;
    }
    What does this function do if we call it with applyeqn(square(x), y, z);? What happens is that the int F(int) is a reference to the function that is passed in as a parameter. Thus inside applyeqn where there is a call to F, it actually is a call to square! This is very useful if we have one set function, but wish to vary the input according to a particular function. So if we had a different function called cube we could change how we call applyeqn by calling the function by applyeqn(cube(x), y, z);.

Post a Comment

Please Select Embedded Mode To Show The Comment System.*

Previous Post Next Post