Main Content

coder.inlineCall

Inline called function in generated code

Since R2024a

    Description

    out = coder.inlineCall(functionCall) evaluates functionCall and inlines the called function in the generated code. The functionCall can take one or more input arguments and can return a single output. Use coder.inlineCall in your MATLAB® code to replace the function call with the body of the called function in the generated code.

    Inlining eliminates the overhead of a function call and can create opportunities for further optimization of the generated C/C++ code. However, inlining can generate larger, more complex C/C++ code. The coder.inlineCall function overrides coder.inline (MATLAB Coder) directives in the called function.

    The coder.inlineCall function does not support the inlining of:

    • Recursive functions

    • Functions that contain parfor-loops

    • Functions called from parfor-loops

    example

    [out1,...,outN] = coder.inlineCall(handle,arg1,...,argN) calls the function with handle handle, which can have one or more input arguments, and inlines this function in the generated code. The specified function can have more than one output.

    example

    Examples

    collapse all

    Create the local function local_NoInline that returns the square of the input value and includes the directive coder.inline("never"). Create the entry-point function useInlineCall that calls local_NoInline using coder.inlineCall to override the coder.inline("never") directive.

    type useInlineCall.m
    function x = useInlineCall(n) %#codegen
    arguments
        n (1,1) double
    end
    x = coder.inlineCall(local_NoInline(n));
    end
    
    function y = local_NoInline(x)
    coder.inline("never");
    y = x^2;
    end
    

    Generate C code for useInlineCall and inspect the entry-point function in the generated code. The code generator inlines local_NoInline, replacing the function call with the body of the function.

    codegen -config:lib useInlineCall
    Warning: Code generation is using a coder.EmbeddedCodeConfig object. Because
    Embedded Coder is not installed, this might cause some Embedded Coder features
    to fail.
    
    Code generation successful (with warnings): To view the report, open('codegen/lib/useInlineCall/html/report.mldatx')
    
    type(fullfile("codegen","lib","useInlineCall","useInlineCall.c"))
    /*
     * Prerelease License - for engineering feedback and testing purposes
     * only. Not for sale.
     * File: useInlineCall.c
     *
     * MATLAB Coder version            : 24.2
     * C/C++ source code generated on  : 20-Jul-2024 16:10:58
     */
    
    /* Include Files */
    #include "useInlineCall.h"
    
    /* Function Definitions */
    /*
     * Arguments    : double n
     * Return Type  : double
     */
    double useInlineCall(double n)
    {
      return n * n;
    }
    
    /*
     * File trailer for useInlineCall.c
     *
     * [EOF]
     */
    

    Create the local function circleMath that takes the radius of a circle as input and returns two outputs: circle area and circumference. Create the entry-point function multiOutputInlineCall that uses coder.inlineCall to call the function handle for circleMath and to instruct the code generator to inline this function.

    type multiOutputInlineCall.m
    function [area, circ] = multiOutputInlineCall(radius) %#codegen
    arguments
        radius (1,1) double
    end
    [area, circ] = coder.inlineCall(@circleMath,radius);
    end
    
    function [a,c] = circleMath(r)
    coder.inline("never");
    a = pi*r^2;
    c = pi*2*r;
    end
    

    Generate C code for multiOutputInlineCall and inspect the entry-point function in the generated code. The code generator overrides the coder.inline("never") directive and inlines circleMath in the generated code, replacing the function call with the body of the function.

    codegen -config:lib multiOutputInlineCall
    Warning: Code generation is using a coder.EmbeddedCodeConfig object. Because
    Embedded Coder is not installed, this might cause some Embedded Coder features
    to fail.
    
    Code generation successful (with warnings): To view the report, open('codegen/lib/multiOutputInlineCall/html/report.mldatx')
    
    type(fullfile("codegen","lib","multiOutputInlineCall","multiOutputInlineCall.c"))
    /*
     * Prerelease License - for engineering feedback and testing purposes
     * only. Not for sale.
     * File: multiOutputInlineCall.c
     *
     * MATLAB Coder version            : 24.2
     * C/C++ source code generated on  : 20-Jul-2024 16:11:01
     */
    
    /* Include Files */
    #include "multiOutputInlineCall.h"
    
    /* Function Definitions */
    /*
     * Arguments    : double radius
     *                double *area
     *                double *circ
     * Return Type  : void
     */
    void multiOutputInlineCall(double radius, double *area, double *circ)
    {
      *area = 3.1415926535897931 * (radius * radius);
      *circ = 6.2831853071795862 * radius;
    }
    
    /*
     * File trailer for multiOutputInlineCall.c
     *
     * [EOF]
     */
    

    Input Arguments

    collapse all

    Function call to evaluate and inline in the generated code, specified as a function name optionally followed by a comma-separated list of one or more arguments in parentheses. The specified function can return at most one output argument.

    Example: coder.inlineCall(myFunction(10,"myString"))

    Example: out = coder.inlineCall(myFunction)

    Handle to a MathWorks® or user-written function. This function can have multiple outputs.

    Example: [out1,out2] = coder.inlineCall(@myFunction)

    Data Types: function handle

    Inputs to the function with handle handle, specified as a comma-separated list of function arguments. The types of the inputs depend on the called function.

    Example: out = coder.inlineCall(@myFunction,10,"myString")

    Extended Capabilities

    C/C++ Code Generation
    Generate C and C++ code using MATLAB® Coder™.

    GPU Code Generation
    Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.

    Version History

    Introduced in R2024a