Main Content

coder.inline

Control inlining of current function in generated code

Description

coder.inline("always") inlines the body of the function in which it is used, directly into the calling locations in the generated code.

The coder.inline("always") directive does not support the inlining of:

  • Entry-point functions

  • Recursive functions

  • Functions that contain parfor-loops

  • Functions called from parfor-loops

coder.inline("never") prevents inlining of the function in which it is used in the generated code. Use the coder.inline("never") optimization directive when you want to simplify the mapping between the MATLAB® source code and the generated code.

The coder.inline("never") directive does not prevent the inlining of:

  • Empty functions

  • Functions that return constant output

To prevent inlining even in these situations, use the coder.ignoreConst function on an input at the function call site in your MATLAB code. For more information, see Resolve Issue: coder.inline("never") and coder.nonInlineCall Do Not Prevent Function Inlining.

Examples

collapse all

You can use coder.inline in flow-control code. If the software detects contradictory coder.inline directives, the generated code uses the default inlining heuristic and issues a warning.

The inline_division function controls inlining based on whether it performs scalar division or vector division:

function out = inline_example(a)
  if (a > 10)
    out = inline_division(a,2);
  else
    out = 5;
  end
end

function y = inline_division(dividend, divisor)

% For scalar division, inlining produces smaller code
% than the function call itself.  
if isscalar(dividend) && isscalar(divisor)
   coder.inline("always");
else
% Vector division produces a for-loop.
% Prohibit inlining to reduce code size.
   coder.inline("never");
end

if any(divisor == 0)
   error("Cannot divide by 0");
end

y = dividend / divisor;

Use coder.inline("never") in a function to prevent inlining in the generated code. In the generated code, calls to the subfcn1 function remains as function calls.

function out = inline_example(a)
  if (a > 10)
    out = subfcn1(a);
  else
    out = 5;
  end
end

function out = subfcn1(a)
  coder.inline("never");
  out = a + 10;
end

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 R2011a

expand all