cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - You can Bookmark boards, posts or articles that you'd like to access again easily! X

User function with variable number of arguments

ValeryOchkov
24-Ruby IV

User function with variable number of arguments

There is a lot of built-in function with different number of arguments - Find, root, log, stack, min, max etc

I am searching one way to create a user function with different number of arguments.

For this problem for example.

I would like to convert molarity of solution into molality. I need for this a user function with name Molality with 2 arguments - temperature and molarity.

I would like to convert mass ratio of solution into molality. I need for this a user function with name Molality with one argument - molarity. See the picture:

NaCl.png

11 REPLIES 11

Unless PTC will provide a way to leave away parameters of a function when it is called and filling the gap with predefined defaults, the only way I can think of is using a vector as argument.

Your function Molality or MassRatio would have just one argument, but in one case the argument is a 2x1 vector, in the other its a single scalar. The function iteself can test what type the actual parameter is and can act accordingly.

Not as intuitive and natural as having really a variable number of arguments, but at least one way to come close to it.

The function call could be either

MassRatio(stack(25 °C, 2 mole/L)) = ...

or

MassRatio(2 mole/kg) = ....

Mathcad checks the argument types.

MC15_screen20.png

Also, you can't register a function with the same name and a different number of parameters.

I see P3 use the same userefi technology as 11-15 years ago, that's terrible.

Werner's suggestion would work if you adopt the following approach (of course units have to be removed in M15):

varargin.PNG

Alan

Werner_E
25-Diamond I
(To:AlanStevens)

You had beaten me by 5 minutes 😉

Using the conditional approach with "if rows(varagin)=0" or "if IsScalar(varagin)" the parameter can be a simple scalar, too.

Viacheslav Mezentsev wrote:

Mathcad checks the argument types.

The argument can be any type. Mathcad will check only how this argument is used.

Se here in a simple example what I was talking about. "parabel" basically has four parameters and simulates the function

f(x,a,b,c):=a*x^2+b*x+c

but if less than 4 values are assigned, missing values are replaced with defaults (c=0, b=0, a=1) beginning with the last one (c).

fn_variable_nr_parameters.png

In this way you can do that, but for userefi functions argument can't be any type (from MC12 and above I think).

I hadn't userfi in mind as its not available in Prime at the time (P2).

Valery use P3 (I suppose) in his example where userefi available. It is the same userefi which we use in MC15.

Mathcad Prime 3.0 Roadmap, Michael Wüst, Mathcad BDM DACh, 15.04.2013

MP3, рис. 1.png

Valery, what do you mean when you say "user function"? Is this a function like a program block or function from the user library?

Viacheslav Mezentsev wrote:

Valery, what do you mean when you say "user function"? Is this a function like a program block or function from the user library?

y(x):=...

Valery Ochkov wrote:

Viacheslav Mezentsev wrote:

Valery, what do you mean when you say "user function"? Is this a function like a program block or function from the user library?

y(x):=...

That would mean you are after a function definition written in Mathcad as a "normal" programming block, right?

So I think at the time we cannot come any closer as with the idea of a vector argument. The function call would at least look normal with 1 scalar argument.

But for your specific application I would personally prefer the way you already have implemented: using the dummy text argument.

Announcements

Top Tags