# Create a function with parameters

Hello,
I would like to create a function with parameters x -> ax for exemple.
The aim is to create later this one x -> a
x^2+b*x+c.

I would like to get back 5x (not ax).
How can I do that ? Thank you for any help.

Hi, Nathalie.

The trick is, don't use global variables! If you have a global a, set it to 5, then later set it to 7, your reporters will turn x into 7x -- whichever value of a comes last.

But when a is an input to a block, a ringed expression inside that block remembers that particular value of a. So

reports a function that multiplies its input by 5, regardless of any later calls for different coefficients.

Oh, the character ↦ in that block, if you don't know it, is pronounced "maps to," and this block is "a maps to (the function) x maps to ax."

This is the Schemely way to solve problems like yours: You don't mess around with the text of a procedure; instead you use a lambda expression (the gray ring) to bind the variable a in the procedure it creates.

I confess that in Logo, which is dynamically scoped, you can't do it this way, and instead you do construct the expression 5x as text before making a procedure out of it. We're going to invent a way to do that, not to replace the Schemely solution to your problem, but for the sake of macros, which have to construct a procedure body out of pieces.

But I am not sure to understand.
I tried to create the block you're talking about :

And my first idea was to create this function :

The block is here

Ih both case, I expected the reporter to be 5x not ax.
I don't understand.

So, when you wrap an expression in a ring, it's not really quite true that the value of the ringed expression is just the expression. That's true enough if there are no free variables in the ring, such as the (a) in your example. But given such a free variable, the ring binds that variable to the closest one in the environment -- in this case, the input to your FUNCTION block. When you call FUNCTION, its parameter (a) is bound to the actual argument value, 5 in your example. So inside that gray ring, a means 5.

So, let's say I do this:

It looks as if f and g are the same:

But they're not!

They're not the same because each of them remembers its own value for (a).

I finally got it !!!
Many many thanks !

Then I finally realised my real quadratic function

I'm so happy !

And now...
if I don't abuse,

I have this function:

And this definition:

But I get those results:

But I expected to get the same result... Why ? Where is my error ?

When you defined your compose function, you made the inputs f and g type Any (Unevaluated). You did that (I am reading your mind) because you tried Reporter and then when you put the x ↦ ( ) x function in, Snap! put a ring around it, and then it didn't work.

But if you compose normal functions, type Reporter is the right thing:

You confused yourself by mislabeling your function-generating function. It really should be called a ↦ x ↦ ax, because it's not a function of x, it's a function of the parameter a that reports a function of x. So you have to compose the result of calling it with the x ↦ x² (which also reports a function of x). So you have to declare the inputs to compose (f and g) type Reporter, and then when you put one of these function-reporting functions in as input, you have to unringify it by hand.

Is that clear? If not I'll try again.

Exactly !
Then, I declared f and g types as reporters.
It works !

Just a remark, I already had to unringify the inputs when f et g were declared as unevaluated.
And did you noticed how I wrote the function of a (I understand better like this):

Anyway, thank you !

But, now I'm wondering what is the purpose of unevaluated types. When am I going to do such a declaration type ?

The canonical example is reporter IF/ELSE. We now have that as a primitive, but if not, you can build it out of command IF/ELSE. See page 53 of the manual.

I think I've understood the difference. Thank you.