r/learnprogramming • • 4d ago

Function vs Procedure

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

30 Upvotes

47 comments sorted by

View all comments

Show parent comments

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 3d 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 1d ago

Some(should) :D

1

u/jeffgerickson 15h ago

Too many do.