Frankly, I'd write this function differently, at least if B is small:
ClearAll[pollard];
Module[{p},
p[n_, i_] := GCD[PowerMod[2, i!, n] - 1, n];
pollard[n_, B_] := p[n, #] & /@ Select[Range[2, B], 1 < p[n, #] < n &, 1]]
I am not entirely sure what your function is intended to return, and how you define "most B iterations" (since your implementation conflicts with your own requirement). Anyway, above code is reformulated to select the smallest 2 <= i <= B which satisfies the Pollard condition, and to return the corresponding factor as a single-item list. If none is found in the range, an empty list is returned.
# -> pollard[#, 4] & /@ Range[100]
(* {1 -> {}, 2 -> {}, 3 -> {}, 4 -> {}, 5 -> {}, 6 -> {3}, 7 -> {},
8 -> {}, 9 -> {3}, 10 -> {5}, 11 -> {}, 12 -> {3}, 13 -> {},
14 -> {7}, 15 -> {3}, 16 -> {}, 17 -> {}, 18 -> {3}, 19 -> {},
20 -> {5}, 21 -> {3}, 22 -> {}, 23 -> {}, 24 -> {3}, 25 -> {5},
26 -> {13}, 27 -> {3}, 28 -> {7}, 29 -> {}, 30 -> {3}, 31 -> {},
32 -> {}, 33 -> {3}, 34 -> {17}, 35 -> {7}, 36 -> {3}, 37 -> {},
38 -> {}, 39 -> {3}, 40 -> {5}, 41 -> {}, 42 -> {3}, 43 -> {},
44 -> {}, 45 -> {3}, 46 -> {}, 47 -> {}, 48 -> {3}, 49 -> {7},
50 -> {5}, 51 -> {3}, 52 -> {13}, 53 -> {}, 54 -> {3}, 55 -> {5},
56 -> {7}, 57 -> {3}, 58 -> {}, 59 -> {}, 60 -> {3}, 61 -> {},
62 -> {}, 63 -> {3}, 64 -> {}, 65 -> {}, 66 -> {3}, 67 -> {},
68 -> {17}, 69 -> {3}, 70 -> {7}, 71 -> {}, 72 -> {3}, 73 -> {},
74 -> {}, 75 -> {3}, 76 -> {}, 77 -> {7}, 78 -> {3}, 79 -> {},
80 -> {5}, 81 -> {3}, 82 -> {}, 83 -> {}, 84 -> {3}, 85 -> {},
86 -> {}, 87 -> {3}, 88 -> {}, 89 -> {}, 90 -> {3}, 91 -> {7},
92 -> {}, 93 -> {3}, 94 -> {}, 95 -> {5}, 96 -> {3}, 97 -> {},
98 -> {7}, 99 -> {3}, 100 -> {5}} *)
Returnand not a comma? Also see the "possible issues" section on theReturndoc page : Return exits only the innermost construct in which it is invoked. Note also thatWhileonly returns Null. – Sjoerd C. de Vries Jul 06 '15 at 14:59