The standard way to increase the counter is as in the first example. Adding a variable to the call and increasing it. I think you are confused due to lack of cycles and the possibility of updating values.
Note that:
repeat(Times) when Times >= 0 -> repeat(0, Times). repeat(Times, Times) -> done; repeat(N, Times) -> do_a_side_effect, repeat(N + 1, Times).
compiles (more or less) the same as (in pseudocode):
repeat(Times) -> while (N < Times) { do_a_side_effect N++ } return done
If you want to copy the result, there are ways to do it.
Either use the list package, or accumulate the result yourself:
loop(File) -> {ok, Fd} = file:open(File), loop(Fd, 0, []). loop(Fd, Count, Acc) -> case file:read(Fd, 80) of {ok, Line} -> Result = do_something(Line, Count), loop(Fd, Count + 1, [Result | Acc]); eof -> file:close(File), {Count, lists:reverse(Acc)}; {error, Reason} -> {error, Reason} end.
Or something similar based on your example.
Edit: returned. Count as part of the return value, as this was important.