# Mathematica beginner question: Function nesting

• Mathematica
• physicsan
In summary, the conversation is about a person who is new to Mathematica and is looking for help in creating a function that combines two existing functions without using copy and paste. They are struggling with the difference between immediate assignment, delayed assignment, and test for equality. They receive helpful advice and are grateful for the quick and thorough assistance.

#### physicsan

Hello,
I'm quite new to Mathematica and stuck at a point, where I have searched for hours to find a solution, but there seems no way out.

Let's say we have two functions,
f1[x_,y_]=x+y
f2[x_,y_]=x*y

How can a build a function f3, which is e.g. the sum of the other functions f1 and f2, i.e. f3[x_, y_]=x+y+x*y, but without copy&paste, but with direct dependency on f1 and f2?

With f3=f1[x,y]+f2[x,y], the functional dependency on x and y is missing. I need a f3, which also has the same input arguments as f1 and f2 (namely x and y).
With f3[x_,y_]==f1[x,y]+f2[x,y] the functional dependency on the parameters is also not working.

I'm really desperate :(

I think that you don't understand the difference between =(immediate assignment), :=(delayed assignment) and == (test for equality). You want to use := in function assignments so that the assignment is not made until you request evaluation. Try the attached notebook - I think it does what you want.

#### Attachments

• Functions.nb
3.8 KB · Views: 431
Hi,

a thousand thanks! This is exactly what I was looking for! Sorry for my dummy question, it really seems so dummy now :s
Thank you again for your profound and amazingly fast help, you really made my day! :)

You're welcome. Glad I could help.

I understand your frustration and the importance of finding efficient solutions to problems. In this case, the solution lies in using the concept of function composition. This means that you can create a new function, let's call it f3, by nesting f1 and f2 within each other. In Mathematica, this can be done using the Composition function.

For example, you can define f3 as follows:

f3[x_,y_]:=Composition[f1,f2][x,y]

This will create a new function f3 that takes in the same input arguments as f1 and f2, and returns the sum of their outputs. You can then use f3[x,y] to get the desired result.

I hope this helps and good luck with your future Mathematica endeavors!

## 1. What is function nesting in Mathematica?

Function nesting in Mathematica refers to the process of using one function as an argument within another function. This allows for more complex and versatile computations to be performed.

## 2. How do I use function nesting in Mathematica?

To use function nesting, simply input the desired function within the argument of another function. For example, if you want to calculate the square root of the absolute value of a number, you would use the syntax Sqrt[Abs[x]]. This nests the Abs function within the Sqrt function.

## 3. Can I nest multiple functions within each other?

Yes, you can nest multiple functions within each other in Mathematica. This allows for even more complex calculations and operations.

## 4. Are there any limitations to function nesting in Mathematica?

While function nesting is a powerful and versatile feature in Mathematica, it is important to note that nesting too many functions or using complex nested functions can cause performance issues and potentially produce incorrect results. It is recommended to test and check your nested functions carefully before using them in important calculations.

## 5. Can I nest user-defined functions in Mathematica?

Yes, you can nest user-defined functions in Mathematica just like built-in functions. This allows for even more customization and flexibility in your computations.