-1

If you go down to assembler level and basic CPU instructions, what is a function? A function is just some block of code that is surrounded by JMP (jump) instructions. So, instruction pointer (instruction that is currently executed) jumps from some other place in program to the start of a function, executes it's code, and then jumps somewhere else.

In this sense, a loop is definitely a function. Only slight difference is that it usually jumps to it's own beginning to check some condition and execute itself again, instead of jumping to some other place (usually place of its call).

ScienceDiscoverer
  • 205
  • 1
  • 3
  • 13

2 Answers2

4

The key aspect of a function (or procedure) is that when it is called, the address it is called from (return address) is recorded so the function can jump back to the caller when it finished. Many processors have special instructions to perform these two common tasks. For example, x86 has call and ret.

A loop does not generally do any of that. Hence, it is not a function in this sense.

fuz
  • 88,405
  • 25
  • 200
  • 352
  • 1
    IMHO the purpose of a **function** is to **return** some value, e.g. a **result** of calculation. Purpose of a **procedure** is to perform a **set of instructions** - algorithm. However, these two concepts are interchangeable in assembly language. They both can return zero, one, or even more results, usually in registers and flags. – vitsoft Apr 03 '21 at 10:07
  • 1
    @vitsoft, it would be a logic error in any language to expect and operate on a return value when none is provided -- the main difference in assembly is that you generally don't get a compile/assemble time error from this. – Erik Eidt Apr 03 '21 at 15:40
  • 1
    @vitsoft: a procedure is (in C terminology) a `void` function - one without a return value. I don't find the distinction important, especially not in this context where the question is whether you can call it and have it return to one of multiple call-sites. (A void function might just have a side-effect on global state, or "return" a value via pointer / reference args. And a non-void function like `printf` might be called mainly for its side-effects, but return a success/error status. Unless your error handling is purely via exceptions, not return values, "procedure" isn't a useful concept) – Peter Cordes Apr 03 '21 at 18:23
2

I think you're minimizing the dynamic nature of returning to its place of call.  I can agree that it's all branching, sure.  However, loops are not functions.

We can't call a loop from multiple other code locations, but we can call a function from anywhere in the code and it will return to its caller, no matter who.

Loops can reference free/unbound variables, but functions can be parameterized, which is important as functions support multiple callers & call sites.

There's no such thing as a dynamic loop stack (loops nest statically), but there is a dynamic call stack and a dynamic call chain of arbitrary depth.

Erik Eidt
  • 23,049
  • 2
  • 29
  • 53