9

I would like to have a CL function with a single argument, but also with a default argument value.

(defun test1 ((x 0))
  (+ x x))

would seem to be the syntax, but it doesn't work. The tutorials I'm seeing have the argument-default form like above only in use with &optional and &key. Is it possible to have just one function argument and it with a default?

2
  • 4
    What's the purpose of a default, if the parameter is not optional? Commented May 1, 2015 at 16:51
  • 5
    Every positional argument with a default has to be optional or else the default would never be used. Commented May 1, 2015 at 18:57

1 Answer 1

20

You need to signal that it is an optional argument:

(defun test1 (&optional (x 0))
   (+ x x))

As written, you have specified an invalid lambda list and should hopefully have seen some diagnostics from the REPL.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.