Home Explore Blog CI



neovim

4th chunk of `runtime/doc/userfunc.txt`
4dc21f00590ca43d4a8135391657600cc5f85e83466b51e70000000100000fa4
 When a positional argument is not
specified at a call, the default expression is used to initialize it.
This only works for functions declared with `:function`, not for
lambda expressions |expr-lambda|.

Example: >
  function Something(key, value = 10)
     echo a:key .. ": " .. a:value
  endfunction
  call Something('empty')	"empty: 10"
  call Something('key', 20)	"key: 20"

The argument default expressions are evaluated at the time of the function
call, not when the function is defined.  Thus it is possible to use an
expression which is invalid the moment the function is defined.  The
expressions are also only evaluated when arguments are not specified during a
call.

								*E989*
Optional arguments with default expressions must occur after any mandatory
arguments.  You can use "..." after all optional named arguments.

It is possible for later argument defaults to refer to prior arguments,
but not the other way around.  They must be prefixed with "a:", as with all
arguments.

Example that works: >
  :function Okay(mandatory, optional = a:mandatory)
  :endfunction
Example that does NOT work: >
  :function NoGood(first = a:second, second = 10)
  :endfunction
<
When not using "...", the number of arguments in a function call must be at
least equal to the number of mandatory named arguments.  When using "...", the
number of arguments may be larger than the total of mandatory and optional
arguments.

							*local-variables*
Inside a function local variables can be used.  These will disappear when the
function returns. Global variables need to be accessed with "g:". Inside
functions local variables are accessed without prepending anything. But you
can also prepend "l:" if you like.  This is required for some reserved names,
such as "version".

Example: >
  :function Table(title, ...)
  :  echohl Title
  :  echo a:title
  :  echohl None
  :  echo a:0 .. " items:"
  :  for s in a:000
  :    echon ' ' .. s
  :  endfor
  :endfunction

This function can then be called with: >
  call Table("Table", "line1", "line2")
  call Table("Empty Table")

To return more than one value, return a |List|: >
  :function Compute(n1, n2)
  :  if a:n2 == 0
  :    return ["fail", 0]
  :  endif
  :  return ["ok", a:n1 / a:n2]
  :endfunction

This function can then be called with: >
  :let [success, div] = Compute(102, 6)
  :if success == "ok"
  :  echo div
  :endif
<
==============================================================================

2. Calling a function ~
						*:cal* *:call* *E107* *E117*
:[range]cal[l] {name}([arguments])
		Call a function.  The name of the function and its arguments
		are as specified with `:function`.  Up to 20 arguments can be
		used.  The returned value is discarded.
		Without a range and for functions that accept a range, the
		function is called once.  When a range is given the cursor is
		positioned at the start of the first line before executing the
		function.
		When a range is given and the function doesn't handle it
		itself, the function is executed for each line in the range,
		with the cursor in the first column of that line.  The cursor
		is left at the last line (possibly moved by the last function
		call).  The arguments are re-evaluated for each line.  Thus
		this works:
						*function-range-example*  >
	:function Mynumber(arg)
	:  echo line(".") .. " " .. a:arg
	:endfunction
	:1,5call Mynumber(getline("."))
<
		The "a:firstline" and "a:lastline" are defined anyway, they
		can be used to do something different at the start or end of
		the range.

		Example of a function that handles the range itself: >

	:function Cont() range
	:  execute (a:firstline + 1) .. "," .. a:lastline .. 's/^/\t\\ '
	:endfunction
	:4,8call Cont()
<
		This function inserts the continuation character "\" in front
		of all the lines in the range, except the first one.

		When the function returns a composite value it can be further
		dereferenced, but the range will not be used then.  Example: >
	:4,8call GetDict().method()
<		Here

Title: Optional Argument Examples, Local Variables, and Calling Functions
Summary
This section provides examples of optional function arguments and their limitations. It explains the use of local variables within functions and accessing global variables. The passage then covers how to call functions using the `:call` command, including the use of ranges and how functions can handle ranges themselves. It illustrates a function that uses line numbers and another that inserts continuation characters within a specified range.