# How to send a function definition to MathLink from C/C++?

In this tutorial some MathLink concepts are introduced. At page 33 in the last lines the author is talking about sending to MathLink a function definition like F[x_] := x^2.

In C/C++ (Unix) I want to to do exactly like that! Which function should I use to put a definition? MLPutFunction? MLPutSymbol?

-
The simplest way is to define the function in the .tm file. –  Szabolcs May 10 '13 at 23:33
You mean templates? But aren't they used to call a C prog from Mathematica? Here I want the opposite –  Andry May 11 '13 at 7:51
Would you please show me an example? –  Andry May 11 '13 at 8:11
Yes, template files are used when calling C functions from Mathematica. You should make it clear in your question that you need the opposite. –  Szabolcs May 11 '13 at 13:27
Looking at your other MathLink questions: why don't you just drive your C program from Mathematica? It would let you write the Mathematica part directly in Mathematica and would save you a lot of trouble. Your C program can still call back to Mathematica when it needs to. I suspect your design is needlessly complicated. Also regarding your other question: you can just send commands as strings when that is actually simpler and the whole command is known beforehand. –  Szabolcs May 11 '13 at 13:55

Section 2.3.1 of the document you linked details how you can send things to the kernel.

The main point here is that you can send a function definition like you send any other Mathematica input. I doesn't matter that it's a function definition. It's just an expression like any other. If you send as an expression, remember its full form: SetDelayed[F[Pattern[x, Blank[]]], Power[x, 2]]

You can send things to the kernel for evaluation in two ways: either as a string or as an expression. Sending as a string is going to be far easier if you know beforehand what you want to send.

There are two ways to send something as string (shown in section 2.3.1 again):

MLPutFunction(link, "EvaluatePacket", 1);

MLPutFunction(link, "EnterTextPacket", 1);