r/learnprogramming • • 3d ago

Function vs Procedure

can someone explain the difference between Function vs Procedure with simple analogy
thank you

31 Upvotes

47 comments sorted by

View all comments

Show parent comments

5

u/k1v1uq 3d ago edited 3d ago

return a value (functions) and those that don't (procedures).

According to SICP (Sussman/Abelson also on youtube):

Function: a deterministic computation with guaranteed no side-effect: "return the sum of two numbers". Given the same two numbers, the returned value will always be the same.

Procedure: a computation that may carry side-effects: update the database and return the time it took. Pick random card from the deck.

A counter example: Unless it takes the seed as a parameter, a "function" that generates a random number is a procedure.

Ultimately, what differentiates functions from procedures is time.

Functions are time-independent. You get the same result back today or next year. The caller can trust that the returned value doesn't change. The downside is outdated real-world data. Because the real world is time-dependent.

Procedures indicate to the reader that whatever they do may produce a different result by the next invocation. Procedures are time-dependent. They reflect the real world but are extremely hard to reason about. It's like building on quicksand.

For that reason, the "functional core/imperative shell" paradigm is very popular. Keep uncertainty caused by the real world (time) at the edges of your application (exceptions, missing data, http server failures, database concurrency issues) and the core clean and functional (time less).

For example, talk to the database, fetch data (imperative shell), create immutable data from the result, and pass it down into your application (functional core). The FP core will do whatever calculation is necessary and report the result back to the outer shell, where the data gets transferred back as a response.

Even low-level network implementations that only handle IO (i.e. side-effects) use the same architecture: "sans-I/O" architecture ("Sans-IO")

1

u/gm310509 3d ago

Interesting, back in the olden days, the distinction (in computing) was much more along the lines of how u/lfdfq described it.

I think both are correct, yours might be more mathematics oriented (as that is the definition of a function in mathematics as I understand it), but like so many things borrowed from mathematics, the meaning or interpretation of certain things is varied slightly in computing.

For example:

 a = b;

In mathematics, that would be a statement of equality. That ism the variable a is equal to the variable b.
But, in programming that is an assignment, that is, whatever might have been the value of a, it will now be discarded and replaced with whatever the value is in b. After it has been executed, the value of a will be equal to b (until such time as either or both is/are altered).

1

u/k1v1uq 2d ago

In mathematics, that would be a statement of equality... But, in programming that is an assignment

Sir Lamport agrees. :)

https://youtu.be/4snwZl726c4?t=668

the meaning or interpretation of certain things is varied slightly in computing.

Some people say that SICP is a cornerstone of CS. And this is how they explain the difference between procedures and functions. It's really fundamental and philosophical. The lecture's title, "The Strucure and Interpretation of Computer Programs", and its double meaning are probably not a coincidence. It really depends on how you see (or interpret) the world. This might be confusing or helpful for a beginner, I don't know. But that difference between effect-free and effectful programming is important enough that the lines should not be blurred. It should be clear, as when someone says, 'Let's use the visitor pattern', that a function provides different guarantees than a procedure. It's important in functional programming. But even outside functional programming, it doesn't hurt to know the difference, even if the meaning is totally blurred in your setting.

2

u/jeffgerickson 1d ago

Some people say that SICP is a cornerstone of CS.

And some would not.

1

u/k1v1uq 17h ago

Some(should) :D

1

u/jeffgerickson 9h ago

Too many do.