Main Content

coder.inlineCall

Inline called function in generated code

Since R2024a

    Description

    example

    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 any 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.

    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"))
    /*
     * File: useInlineCall.c
     *
     * MATLAB Coder version            : 24.1
     * C/C++ source code generated on  : 12-Feb-2024 20:58:38
     */
    
    /* 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 multiOuputInlineCall that uses coder.inlineCall to call the function handle for circleMath and to instruct the code generator to inline this function.

    type multiOuputInlineCall.m
    function [area, circ] = multiOuputInlineCall(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 multiOuputInlineCall 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 multiOuputInlineCall
    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/multiOuputInlineCall/html/report.mldatx')
    
    type(fullfile("codegen","lib","multiOuputInlineCall","multiOuputInlineCall.c"))
    /*
     * File: multiOuputInlineCall.c
     *
     * MATLAB Coder version            : 24.1
     * C/C++ source code generated on  : 12-Feb-2024 20:58:42
     */
    
    /* Include Files */
    #include "multiOuputInlineCall.h"
    
    /* Function Definitions */
    /*
     * Arguments    : double radius
     *                double *area
     *                double *circ
     * Return Type  : void
     */
    void multiOuputInlineCall(double radius, double *area, double *circ)
    {
      *area = 3.1415926535897931 * (radius * radius);
      *circ = 6.2831853071795862 * radius;
    }
    
    /*
     * File trailer for multiOuputInlineCall.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