# Functions

This project is a little function library where you save and run you functions, while making the project or running the project (Yes you can make some functions while running the project)

You enter a slot number (0-100) then code the code and insert it into the Define block then click it, the block should say Operation: cscautl to mean the code was saved successfully.

So... they can't be recursive, then?

what do you mean recursive?

Ah! I get to teach you something. :~)

A recursive procedure is one that calls itself. Here's the classic example, for the factorial function that computes the product of all the positive integers up to its input:

This works because factorial(4) is 4×3×2×1 = 4×(3×2×1) = 4×factorial(3).

In order for a recursion to work there has to be a base case that the procedure can handle without calling itself; in this case we build in that factorial(1)=1. Otherwise the procedure would keep calling itself forever and never return.

It's important to understand that variables in a recursive procedure, such as number in this example, don't belong to the procedure itself; they belong to an invocation (a call) of the procedure. The way computers work is that inside the computer are a lot of really tiny little people. When you ask for factorial(4), you hire Francine, a factorial expert, and give her a 4 to put in her number pocket. Francine hires other little people for other procedure calls; for example, she hires Edgar to compute the = test, and Isabel for the if, and so on. But what's important for our story is that she hires Fred to compute factorial(3); she gives Fred a 3 to put in his number pocket. He hires a bunch of other little people, including Fanny to compute factorial(2); she hires Ferdinand to compute factorial(1). Ferdinand just hands 1 back to Fanny. Fanny, who still has 2 in her pocket, multiplies Ferdinand's 1 by her 2 and gets 2, which she reports to Fred, who multiplies her 2 by the 3 in his pocket to get 6, which he reports to Francine. She still has 4 in her pocket, so she can multiply Fred's 6 by her 4 to get 24, which she puts in a speech balloon to show you, because you hired her back at the beginning of this paragraph.

Recursion is especially important for things like fractal trees, where you have to make more than one recursive call (one for each branch of the tree). You can't do that just by writing loops -- or I should say, you can, but it's a real pain.

Could recursion work by having the function run n and save it in n?

I'm not sure I understand what you're envisioning. But the variable is a different variable in each invocation, so it can't be part of the function. It has to be part of the invocation.

n is a slot [1, 100] where functions are saved.
If a function is saved in slot 1 and the function runs slot 1, then it will run itself as long as slot 1 isn't changed.

Do you mean that there would be lots of copies of the same procedure?

this is a way for recursion (by changing the c-loop to a ring, and adding with inputs to the run block)

I meant what @ego-lay_atman-bay has in his image above, but the only problem is that there is no "run function with".

Workaround:
[scratchblocks]
(call (Report code (1) :: motion) with ((number) - (1)) :: control)
[/scratchblocks]

Oh I see the problem. You're answering the question, how can the procedure refer to itself? And, sure, by using the same number it's in.

I was answering a different question, about the procedure's local variables (including inputs). Those variables can't belong to one of the numbered slots; they have to belong to a particular invocation of that procedure.

Oh, I understand now; yes, I think there will be different copies of the procedure in the stack.

Different copies of the procedure's variables; the procedure's text doesn't have to be copied, of course.

The procedure needs to be defined with formal parameters; that is, it needs a way to know that number refers to its input, not a global variable.

The gray ring has a parameter called number, which I understand would be copied to another stack frame during the recursion.

Oh oops I totally missed that! Sorry...