function.rst 1.5 KB

123456789101112131415161718192021222324252627282930313233343536
  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. Referencing to ``ARGV#`` arguments beyond ``ARGC`` have undefined
  25. behavior. Checking that ``ARGC`` is greater than ``#`` is the only way
  26. to ensure that ``ARGV#`` was passed to the function as an extra
  27. argument.
  28. A function opens a new scope: see :command:`set(var PARENT_SCOPE)` for
  29. details.
  30. See the :command:`cmake_policy()` command documentation for the behavior
  31. of policies inside functions.