It is of course possible to redefine functions within loops in Mathematica. You are actually just missing a semicolon at the right place for your code to work as intendend:
For[i = 1, i <= 5, i++,
f[x_] := Sin[x]^2;
Print[{i, f[i]}]
]
It's probably worth noting (as Jacob did in his comment) that the semicolon is just a shortcut for a CompoundExpresson, so something like a;b;c looks like this in FullForm: CompoundExpression[a,b,c], which you can check with e.g. FullForm[Hold[a;b;c]]. As the default evaluation order is to evaluate arguments from left to right, you could actually use any other symbol without (matching) definitions and Hold attributes instead of CompoundExpression, which is why e.g. List in the other answer also works, but of course using CompoundExpression is much more standard and clear. The main difference is that CompoundExpression returns the return value of its last argument, which other symbols won't do, the following might make these subtleties more clear:
Print[a];Print[b];c
CompoundExpression[Print[a],Print[b],c]
{Print[a],Print[b],c}
List[Print[a],Print[b],c]
somethingelse[Print[a],Print[b],c]
Without that semicolon (CompoundExpression) what you do is to redefine f[x_] to Sin[x]^2*Print[{i,f[i]}] in every loop pass without ever using that function. This is also the reason why you don't see any output. As it is perfect valid code there is no reason to expect any error or even warning messages - Mathematica can't (yet) guess that this is not what you intended...
You'll often find that people tell you to not use loops in Mathematica, but I think there are many cases where using loops is the right thing to do. You'll find that other constructs will give you better performance, less typing and still clearer code in many cases, though. Learning those other possibilities will be of great advantage if you plan to use Mathematica more often in the future. For reasons mentioned e.g. in answers to this question I'd strongly suggest to avoid For loops, though. Here is the same thing using a Do loop:
Do[
f[x_] := Sin[x]^2;
Print[{i, f[i]}],
{i,5}
]
You should also learn about the difference of Printing a result and returning it. In almost all cases you want to return what you calculate and not just print it, and thus you'd probably rather use something like this:
Table[
f[x_] := Sin[x]^2;
{i, f[i]},
{i,5}
]
Table). E.g. you could work with aPiecewisefunction. But it is difficult to give more exact hints without knowing the problem better. Btw: You do not get an error message, because your code is not really wrong (the syntax is correct – but not the logic). Run it and check the result off[1], then you see that your code worked – just in a way you do not want it to work. – partial81 Apr 26 '13 at 12:07for i in range(1,10): def ABC(y): print(y) ABC(i)Of course I can. Just get the indent right. – Misery Apr 26 '13 at 18:10