4 Start recording a function for later invocation as a command.
8 function(<name> [<arg1> ...])
12 Defines a function named ``<name>`` that takes arguments named
13 ``<arg1>``, ... The ``<commands>`` in the function definition
14 are recorded; they are not executed until the function is invoked.
16 Per legacy, the :command:`endfunction` command admits an optional
17 ``<name>`` argument. If used, it must be a verbatim repeat of the
18 argument of the opening ``function`` command.
20 A function opens a new scope: see :command:`set(var PARENT_SCOPE)` for
23 See the :command:`cmake_policy()` command documentation for the behavior
24 of policies inside functions.
26 See the :command:`macro()` command documentation for differences
27 between CMake functions and macros.
32 The function invocation is case-insensitive. A function defined as
40 can be invoked through any of
47 cmake_language(CALL foo)
49 and so on. However, it is strongly recommended to stay with the
50 case chosen in the function definition. Typically functions use
53 .. versionadded:: 3.18
54 The :command:`cmake_language(CALL ...)` command can also be used to
60 When the function is invoked, the recorded ``<commands>`` are first
61 modified by replacing formal parameters (``${arg1}``, ...) with the
62 arguments passed, and then invoked as normal commands.
64 In addition to referencing the formal parameters you can reference the
65 ``ARGC`` variable which will be set to the number of arguments passed
66 into the function as well as ``ARGV0``, ``ARGV1``, ``ARGV2``, ... which
67 will have the actual values of the arguments passed in. This facilitates
68 creating functions with optional arguments.
70 Furthermore, ``ARGV`` holds the list of all arguments given to the
71 function and ``ARGN`` holds the list of arguments past the last expected
72 argument. Referencing to ``ARGV#`` arguments beyond ``ARGC`` have
73 undefined behavior. Checking that ``ARGC`` is greater than ``#`` is
74 the only way to ensure that ``ARGV#`` was passed to the function as an
80 * :command:`endfunction`