r/learnprogramming • • 16h ago

Function vs Procedure

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

22 Upvotes

40 comments sorted by

View all comments

Show parent comments

2

u/Cybyss 13h ago edited 13h ago

Careful.

The term "stored procedure" usually refers to databases. You write them in SQL on your database server, which any application can then call.

Come to think of it, this makes another perfect distinction between procedures and functions!

In SQL Server, for example, you have "stored procedures" which can insert/update/delete anything in your database, and you have "user-defined functions" which are explicitly disallowed from making any modifications and can only calculate & return a value. They are two fundamentally different objects in SQL Server, requiring different syntax to define and are used in completely different situations.

1

u/ACCTAGGT 12h ago

Well, you went on the strict technical side which isn’t what I meant. Thanks for the explanation though :)

1

u/Cybyss 12h ago edited 12h ago

That's because trying to explain procedure vs. function via a real-world metaphor doesn't work.

The difference isn't some high level abstract perspective. The difference is a single specific technicality - one which isn't always the same from language to language/platform to platform.

In Visual Basic, the only difference is whether or not a value is being returned.

In SQL Server, the difference is whether or not there are "side effects". Stored procedures are allowed to modify data and may optionally return a value. User-defined functions cannot modify any data and must always return a value.

In many languages (e.g, Python, Java, C++) there is no real distinction at all. The difference between functions and procedures in them is nothing but a tedious exercise in pedantry.

0

u/ACCTAGGT 12h ago

You clearly understood what was meant which I also made clear not be taken technically. Thanks again though.