function.rst 1.3 KB

1234567891011121314151617181920212223242526272829303132
  1. function
  2. --------
  3. Start recording a function for later invocation as a command::
  4. function(<name> [arg1 [arg2 [arg3 ...]]])
  5. COMMAND1(ARGS ...)
  6. COMMAND2(ARGS ...)
  7. ...
  8. endfunction(<name>)
  9. Define a function named ``<name>`` that takes arguments named ``arg1``,
  10. ``arg2``, ``arg3``, (...).
  11. Commands listed after function, but before the matching
  12. :command:`endfunction()`, are not invoked until the function is invoked.
  13. When it is invoked, the commands recorded in the function are first
  14. modified by replacing formal parameters (``${arg1}``) with the arguments
  15. passed, and then invoked as normal commands.
  16. In addition to referencing the formal parameters you can reference the
  17. ``ARGC`` variable which will be set to the number of arguments passed
  18. into the function as well as ``ARGV0``, ``ARGV1``, ``ARGV2``, ... which
  19. will have the actual values of the arguments passed in.
  20. This facilitates creating functions with optional arguments.
  21. Additionally ``ARGV`` holds the list of all arguments given to the
  22. function and ``ARGN`` holds the list of arguments past the last expected
  23. argument.
  24. A function opens a new scope: see :command:`set(var PARENT_SCOPE)` for
  25. details.
  26. See the :command:`cmake_policy()` command documentation for the behavior
  27. of policies inside functions.