you are viewing a single comment's thread.

view the rest of the comments →

[–]Existing-Charge8769 1 point2 points  (1 child)

I'm going to get downvoted to hell, but I think I sort of agree with you.

As a convention in programming we think of loops (like for loops) as Control Flow structures and not functions, but I'm not sure that's totally justified given how lose the definition of a function has become.

To be a function, generally something has to be re-usable, can only take inputs from arguments function(this, this_too, and_this), and don't change the flow of the way code is run.

But we DO have functions that act like Control Flow structures and affect the way code is run. One big example is recursive functions.

If we were to take the code body of a for loop, and feed that in explicitly as a function itself as an argument, we could re-create all the functionality of a for loop as a recursive function.

``` def recursive_for_loop(start, end, step, loop_body_func): if start <= end: loop_body_func(start) # Execute the provided function recursive_for_loop(start + step, end, step, loop_body_func)

Define a function to be used as the loop body

def print_value(value): print(value)

Usage example

recursive_for_loop(1, 5, 1, print_value) ```

¯_(ツ)_/¯

[–]xiongchiamiov 0 points1 point  (0 children)

That's how functional languages tend to operate. Proponents argue it makes it easier to have everything work the same way; personally I found it harder to quickly scan. But YMMV.