Functions with Arguments
 
 
 

With AutoLISP, you can define functions that accept arguments. Unlike many of the standard AutoLISP functions, user-defined functions cannot have optional arguments. When you call a user-defined function that accepts arguments, you must provide values for all the arguments.

The symbols to use as arguments are defined in the argument list before the local variables. Arguments are treated as a special type of local variable; argument variables are not available outside the function. You cannot define a function with multiple arguments of the same name.

The following code defines a function that accepts two string arguments, combines them with another string, and returns the resulting string.

_$ (defun ARGTEST (
arg1 arg2 / ccc )
(_> (setq ccc "Constant
string")
(_> (strcat ccc ", "
arg1 ", " arg2) )
ARGTEST 

The ARGTEST function returns the desired value because AutoLISP always returns the results of the last expression it evaluates. The last line in ARGTEST uses strcat to concatenate the strings, and the resulting value is returned. This is one example where you should not use the princ function to suppress the return value from your program.

This type of function can be used a number of times within an application to combine two variable strings with one constant string in a specific order. Because it returns a value, you can save the value to a variable for use later in the application.

_$ (setq newstr (ARGTEST
"String 1" "String 2") ) 
"Constant string, String 1, String 2"

The newstr variable is now set to the value of the three strings combined.

Note that the ccc variable was defined locally within the ARGTEST function. Once the function runs to completion, AutoLISP recycles the variable, recapturing the memory allocated to it. To prove this, check from the VLISP Console window to see if there is still a value assigned to ccc.

_$  ccc
nil