## How to define a function with default arguments?

2014-03-26
2014-03-31
• I need to define a function with default arguments.
I came up with something like

```fn(x,[a]):=
/* multiply x by a */
/* default value of a: 1 */
block(
if emptyp(a) then a: 1 else a:a[1],
a*x
)\$

fn(1);
fn(1,1);
fn(1,2);

(%o31) 1
(%o32) 1
(%o33) 2
```

What is the right way to define such functions?
Wolfgang

• Ago77
2014-03-28

f: lambda ([[x]], apply ("*", x));

f(4);

f(4, 5);

• I don't think, this answers my question.
fn(x,[a]):=
/ multiply x by a /
/ default value of a: 2 /
block(
if emptyp(a) then a: 2 else a:a[1],
a*x
)\$

What i really need -maybe better like to have- is one ore more default values in more complicated situations. Think of

```f(x,x_0,n):=(x-x_0)^n;
```

versus

```g(x,[defaults]):=
block(
[x_0:0, n:1],
if not(emptyp(defaults)) then(x_0: defaults[1], n: defaults[2]),
(x-x_0)^n
)\$

g(x);
g(x,1,5);
(%o14) x
(%o15) (x-1)^5
```

I don't think this is the right way to define functions with default values. That's the reason for my question.

Wolfgang.

2014-03-30

Hello

I am not from wxMaxima team, but my guess is that what you want is for wxMaxima to somehow guess your thoughts without you needing to define them. To me, it seems that your first approach is the minimum needed in order to let the program know about your intentions. That is, you have to define the function itself, a*x, then you have to specify the default value of a if it's not defined or present in the function call. The equivalent of your approach would be:

f(x,[a]):=(if emptyp(a) then a:2 else a:a[1])*x;

Your second approach can be defined in the same way. Maybe what seems too long is the way "if" needs to be expressed, as "if {} then {} else {}".

• "my guess is that what you want is for wxMaxima to somehow guess your thoughts without you needing to define them."

I don't think default arguments have something in common with guessing. In my opinion they give a clear way to express, what needs to be expressed - and nothing more.

I know, that f(x,[a]) makes a to be an optional list of arguments. I forced one or two of these optional arguments to have default values if i don't specify them by using the if-construct. I would like to know, whether there is a maxima construct to have default arguments in a function or not. In common lisp, you can write i.e. (found on http://www.gigamonkeys.com/book/functions.html):

```(defun foo (a &optional (b 10)) (list a b))

(foo 1 2) ==> (1 2)
(foo 1)   ==> (1 10)
```

This makes b an optional argument with default value 10. I am looking for a clear way to define such functions in maxima. Maybe there is no such way. That would be an answer, too.

Wolfgang

• Here's a hack that does what you want. It requires that the argument to the function be a list, so your original "x" is x[1], and the multiplicative factor "a" is in x[2], which has a default value of %pi.

```f(x):=block(
if length(x) < 2 then x:append(x,[%pi]),
x[1]*x[2]
)\$
```

• Ok. Thank you for your information.

Wolfgang