Skip to main content
1-Visitor
April 27, 2014
Question

Neater Functions

  • April 27, 2014
  • 2 replies
  • 3072 views

Hi all,

A question regarding functions...

If I have two functions, say:

bob(one, two, three) := ..............

sue(four,five,six) := .................

If I want to define a third function which is a combination of these, say:

tom(bob(one,two,three),sue(four,five,six)) := .....................

If I want to vary any of the arguments in either bob or sue then I have to include them both in tom. The trouble is, with more complex funcitons with lots of arguments, and in cases where a higher level function depends on lots of others, it's easy for the last function to become a bit unweildy and out of hand and hard to keep track. It also doensn't look very good!

Is there a clever way around this? Or is there a better way which I'm just not aware of?

Thanks.

2 replies

25-Diamond I
April 27, 2014

If I want to vary any of the arguments in either bob or sue then I have to include them both in tom.

?????

I think its best if you post a worksheet with a complete example of what you are trying to do.

Why not tom(a,b,c,d,e,f,g):=... and let tom call bob() and sue() ?

1-Visitor
April 27, 2014

Hi Werner,


Thanks for your response.

I guess one question I have is.... is there a difference between

tom(one,two,three,four,five,six) := function of bob and sue

and

tom(bob(one,two,three),sue(four,five,six)) := function of bob and sue

Will there be a difference in the answers to these if I vary the arguments? Are there advantages to using one approach over the other?

19-Tanzanite
April 27, 2014

One option would be to put your input arguments into a vector and just pass the function names and the vector to the final function. e.g. v = (one, two, three,four, five, six) and call tom(bob,sue,v).

This won't work in M15 and earlier if you are using units and they are not all the same.

Alan

1-Visitor
April 27, 2014

Thanks for your responses guys.

At the moment I'm just trying to get my head around exactly how functions work which is why I don't have a concrete example.

So, for example, if I have a function:

tom(x,y,bob):=bob(x,y)+x+y

bob(x,y):=..........

I'm assuming that mathcad uses the arguments I included in tom() to evaluate bob? (i.e. it doesn't matter if I define x and y above in the sheet, it will use those that I include as inputs to tom). Does it matter what order the arguments come in? Or does mathcad read all of the arguments first and then use this to evaluate the function tom()?

I of course could work this out by playing around with mathcad itself but I'm seeking any extra answers/tips/understanding I might get by connecting with the forum.

Cheers.

25-Diamond I
April 27, 2014

Why not simply define

bob(x,y):=.....

tom(x,y):=bob(x,y)+x+y

When doing so you have to define bob() above tom().

If you do it as you have show, you wouldn't need to use "bob" as name in the definition of tom() as you will provide the function name not until you call tom().

And yes, you are right, the x and y (and in your example also bob) are just formal arguments. Their name does not matter as long as you use the appropriate names at the RHS and LHS of the definition. It also does not matter if x and y are defined in your sheet and what value they have. The value is determined by the actual arguments not until you call your function.

x:=5 y:=5 z:=5

tom(x,y,f):=f(x,y)+x+y+z

bob(x,y):=x+y

sue(x,y):=x-y

y:=2 z:=100

tom(2,3,bob) =

tom(3,2,bob) =

tom(x,y,sue) =

tom(bob(2,3),sue(2,3),bob) =

tom(bob(2,3),sue(2,3),sue) =

Try to find out the result of the last four evaluation and then verify using Mathcad to check if you have understood the underlying principles.

You might also want to compare tom() to cat():

bob(x,y):=x+y

sue(x,y):=x-y

cat(x,y):=bob(x,y)+x+y

cat(2,3) =

cat(bob(2,3), sue(2,3)) =

.....

1-Visitor
April 28, 2014

Thanks!

I'm finding some of my functions are getting ridiculously long, because they are functions of other (also long) functions. I like the idea of using a vector to input data but I don't like that the units then have to be reinserted...

I suppose this is something I just have to live with as I can't see any other clever tricks to get around it