# Function that takes another function inside

Looking at this QA, I'm trying the following - 2 functions (r in vector format) using Theta { A, 2 } functions (sorry for the inconsistent names...).

ihat = {1, 0, 0}
jhat = {0, 1, 0}
khat = {0, 0, 1}
ThetaA[t_] := 0.2 Pi Cos[50 t];
Theta2[t_] := 0.2 Pi Sin[50 t - Pi/3];
L1 = L2 = 5.0;
rAO[ThetaA_, t_] := L1 Sin[ThetaA[t]] ihat + L1 Cos[ThetaA[t]] jhat
rBA[t_] := (L2 Sin[Theta2[t]] + rAO[]) ihat
+ (L2 Cos[Theta2[t]] + rAO[]) jhat


which gives me the following errors: Could anyone tell me what's wrong with my functions. Thank you.

• Looks like maybe r_(A/O) and r_(B/A) had values before you tried to turn it into a function. Feb 6, 2012 at 2:10
• Please don't post images of code you would like others to evaluate. Reduce your question to a minimal example, and then write the code of that in your post. Feb 6, 2012 at 2:18
• Not as extreme as @David's position, but: you're asking us for help to debug your code. Please do bother to take time to post something, anything, that can be easily copied into Mathematica in your question. The image only serves as an additional diagnostic, and other than that, it's not something we can debug... Feb 6, 2012 at 2:45
• Just so that my position is glaringly clear: a screenshot is nice to have; copyable code is need to have. Feb 6, 2012 at 2:50
• I copied and formatted your code as the picture of it would not help anyone who happens to run across it later. In the process, I removed all of the Mathematica markup as it would have made the code very difficult to read. Feb 6, 2012 at 3:19

The reason you're getting that error is because you're using O, which is a built-in function to represent a term of a particular order. This has the attribute Protected, which prevents you from assigning any definition to it.

Attributes@O


However, to answer the question in your title, here are a few simple ways in which you can use a function inside another function:

### 1: Simply call the function!

f[x_] := x^2
g[x_] := Sin[f[x]] ### 2: Use a Module

g[x_] := Module[{f},
f[y_] := y^2; Sin[f[x]]
] ### 3: Pass the symbol for the function

f[x_] := x^2
g[func_Symbol, x_] := Sin[func[x]] • I think Clear did the work for me (now have another error), and it makes me realize that capital 'O' is reserved by Mathematica. Anyway thanks! Feb 6, 2012 at 2:43
• @IsaacSaito Yes, that's a built in function. I've added a link to the documentation. I was a bit hesitant at first, because I wasn't sure if it was o or O, which is where a copyable code would've helped :)
– rm -rf
Feb 6, 2012 at 2:57
• We probably should have a list of functions like D[], N[], and O[] stashed here somewhere... ;) Feb 6, 2012 at 3:04
• @J.M. or maybe just remind people not to use capital letters to name variables and functions... a pretty useful lesson for those new to Mathematica. Feb 6, 2012 at 4:50