# Button with for loop

How can I call a function that has a for loop inside a button? If I just write

button[{"name", function[]}]


nothing happens.

-
I wonder why... What exactly is the function[]? And skip the curly brackets, I don't think they're necessary here. –  Wojciech Dec 16 '13 at 23:17
Basically it's a function with a for loop. I'm trying to put a simples for with a print, and it seems that only the first 40s numbers are being executed. –  Zhozer Dec 16 '13 at 23:32
Please put all relevant information in the question, rather than in comments. Your posted code contains obvious syntax errors (lowercase "b" and button arguments in a list), and the question states that "nothing happens" when in fact you say in a comment that the loop runs for the first 40 iterations. Most likely your code is timing out and you need to use the Method -> "Queued" option. –  Simon Woods Dec 17 '13 at 14:28

## 1 Answer

It's hard to tell exactly what the problem is, but here is one way:

f[n_] := Print[Range[n]];
Button["name", f[7]]


Now when you press the button, it prints from 1 to 7 (or whatever number you place in the f[ ]. The action of the function f is invoked whenever you press the button, so it could set values, print, graph things, define variables, and generally do anything that a function does.

If you really want a For loop in the function, that's easy too:

g[n_] := For[i = 0, i < n, i++, Print[i]];
Button["name", g[7]]

-