Why use macros instead of functions?

Inside a Chialisp program (not CLVM), I don’t understand the difference between defining a macro and a function. When does it make sense to define a macro instead of a function?

The Squaring a List program looks simpler when square is defined a function:

(defun-inline square (input)
  (* input input)
)

rather than when it’s defined as a macro:

(defmacro square (input)
  (qq (* (unquote input) (unquote input)))
)

Note: both versions of the Squaring a list program result in identical CLVM.

1 Like

Macros allow you to manipulate the arguments at compile-time instead of run-time, allowing for constructs such as and. Inline functions are a simplified form of macros that just convert to an expression.

1 Like