function

function — creates a new function.

Syntax

function name ([argument [,argument]... ]) statement

		

Arguments

name

The name of the function.

argument

A symbol that names the argument.

statement

The body of the function.

Returns

A named function definition in Lisp syntax. When a function is called, the return value is the value of the last expression to be evaluated in the function body.

Description

The function statement declares a new function. All function arguments are implicitly local to the scope of the function. The argument list is denoted by parentheses, and contains zero or more argument definitions, separated by commas. Each argument can be a symbol, which is the name of the argument, along with any combination of the following modifiers:

[excl ]  before the argument indicates that this argument will not be evaluated when the function is called.

[quest ]  after the argument indicates that this argument is optional. Only the first optional argument has to be marked as optional. All arguments after that are implicitly optional. An optional argument may have a default value, specified by appending = expression after the question mark.

[Note]

The only way to test whether an optional function parameter has been provided is by using the predicate undefined_p, which tests for the _undefined_ value.

...   after the argument indicates that this argument is a "catch-all" argument used to implement variable length argument lists. Only the last argument in the argument list can have ... after it. An argument modified by ... will always be either nil, or a list containing all arguments from this position onward in the function call.

When a function is called, its arguments are bound in a new local scope, overriding previous definitions of those symbols for the duration of the function.

Example

This function, with one argument, returns an integer at least one greater than the argument.

function next (n)
{
	ceil(n) + 1;
}

This function prints its first two arguments and optionally prints a new line (which is printed by default). It returns a string concatenation of the first two arguments.

function print_two (first, second, newline?=t)
{
	princ(first, " ", second);
	if (newline)
		terpri();

	string(first, " ", second);
}

This function adds all of its arguments. It insists on having at least one argument. Notice that the optional character '?' and the rest character '...' are combined in the second argument.

function add_all (first, others...?)
{
	local sum,x = 0;
	sum = first;
	if ( !undefined_p(others) )
	{
		for(x=others;x;x=cdr(x))
			sum = sum + car(x);
	}
	sum;
}

See Also

method, Statements