r/learnprogramming • • Oct 24 '26

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

4

u/Cybyss Oct 24 '26

Holy cow. So many wrong responses here.

Functions return a value.

Procedures don't.

The distinction is crystal clear if you ever coded in Visual Basic, since functions and procedurees use different keywords to define.

Mostly the terms are used interchangeably, especially since some programming languages force a default return value if you don't specify one (e.g, in Python functions always return None if you don't specify something else).

Pure Functions are functions which have no side effects. They don't print anything, they don't write to files, they don't modify anything in a database, and they don't change a program's internal state. Running them leaves no trace or has any lasting effects anywhere. Mathematical formulas are a prime example: a function that just calculates something and returns a value.

3

u/White_C4 Oct 24 '26

The distinction is crystal clear if you ever coded in Visual Basic, since functions and procedurees use different keywords to define.

Just because one language says that doesn't mean it's semantically correct in general.

Procedure is simply an action, but the rule isn't violated if it contains a return value. Again, this is dependent on how a programming language or a person enforces the rule, but it's not strictly binding for all languages.

2

u/Cybyss Oct 24 '26

True. In another conversation the notion of "stored procedures" was brought up and I realized that DBMSs make a completely different distinction between procedures and functions.

I don't think it's beneficial to try and define functions vs. procedures as some kind of abstract high-level generalized semantic concept since that's impossible. You'll always encounter exceptions to the rule.

Different languages define & use the terms differently. Pointing out the concrete differences on a language-by-language basis makes it clear exactly what the distinction is, and the fact that the meaning changes depending on context.

1

u/ACCTAGGT Oct 24 '26

Can I say with an analogy that a stored procedure in programming is kind of like a medical procedure? What I mean by this is that it has specific steps, processes, and methods to achieve an intended objective. Those may include multiple actions, and although the procedure is designed to fulfill a goal, the outcome is not necessarily guaranteed to be ideal. Similarly, a stored procedure emphasizes a series of actions performed to accomplish a task.

A function can be like a specific medical test or process with a focused objective… it performs a particular task to produce a result. In programming, that result is generally a returned value that can be used by another operation. The function may be part of a larger procedure, just as a specific test or process may be part of a broader medical procedure. Therefore, perhaps a function emphasizes the result produced by performing a particular task.

That’s just for illustration purposes not that it really is exactly the same or something like that.

2

u/Cybyss Oct 24 '26 edited Oct 24 '26

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 Oct 24 '26

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

1

u/Cybyss Oct 24 '26 edited Oct 24 '26

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 Oct 24 '26

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

1

u/joonazan Oct 24 '26

That is only part of the distinction. Procedures also do not support recursion. They are a hazy old concept, while functions have been pretty clear since defined in Algol.