Skip to main content
Engineering LibreTexts

6.5: Functions of Functions - Handles

  • Page ID
    55659
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    It is often the case that we wish to pass a function to a function: in other words, we wish a called function to be able to operate not just on different data but also on different "input functions." To make matters more concrete (and avoid using the word function too many times with reference to different entities), consider the function f_o_diff:

    function [ value ] = f_o_diff ( func, x, delta_x ) 
    value = (func (x + delta_x) - func (x))./delta_x; 
    end
    

    This little function calculates the first-order finite difference approximation to a function func at the point \(\mathrm{x}\) for a given segment-length delta_ \(\mathrm{x}\). Obviously we could include the definition of func within f_o_diff, but then we would need to have a different derivative function for each function we wished to differentiate. In contrast, \(f_{\text {_o_diff can be re-used for any function func }}\) - clearly much preferred. (Note we could now perform a much more systematic investigation of round-off error; in our earlier discussion we were not yet armed with functions, or arrays.)

    To call \(f_{-} \circ\) _iff from a calling program is quite simple with only one wrinkle within the MATLAB syntax. In particular, to pass the input function func from the calling program to the called function (f_o_diff) we do not wish to actually pass the function but rather a kind of pointer - or handle - to where these instructions are stored for use by any (calling) program. (The description here is virtual - a mental model which provides the right intuition. In general, what and how a programming language passes within a function call can be a rather complicated issue.) To create a handle for the function func - in other words, to find the pointer to (say) the beginning of the set of instructions which define func - we put an "at sign" (@) in front of func as in @func. So for example, to apply \(f_{-}\)_diff to the MATLAB function sin we can either do

    >> sin_handle = @sin; 
    >> fprime = f_o_diff( sin_handle, [pi/4, pi/2], .01) 
    fprime = 
        0.7036 -0.0050 
    >>
    

    of more directly

    >> fprime_too = f_o_diff( @sin, [pi/4, pi/2], .01) 
    fprime_too = 0.7036 -0.0050 
    >>
    

    Note handles can also be created for other kinds of objects, for example (graphics) figures.

    It is often the case that a function func we wish to pass to (say) function_name is somehow more general - defined with respect to more inputs - than the functions which function_name expects. In MATLAB there is an easy way to deal with this common occurrence, which we now discuss.


    This page titled 6.5: Functions of Functions - Handles is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Masayuki Yano, James Douglass Penn, George Konidaris, & Anthony T Patera (MIT OpenCourseWare) via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.