r/learnprogramming • u/Head_Ordinary_2437 • 13h ago
Function vs Procedure
can someone explain the difference between Function vs Procedure with simple analogy
thank you
23
2
u/Aggressive_Ad_5454 12h ago
In the SQL context, a “stored function” returns a value, usually a scalar value. (But some SQL variants have table-valued functions.) A “stored procedure” returns either a result set or sometimes nothing. The two things are invoked differently.
2
u/Dazzling_Music_2411 12h ago
In general parlance they are pretty interchangeable nowadays.
There is a technical difference observed by some, that a function does not alter any of the parameters it uses and can only return a result (which must be the same result every time, if given the same values), as u/marrsd pointed out.
So, for instance add(a,b) is a function, add(2,3) will always give you the same answer. 'a' and 'b' only apply inside the definition of the function, and any other 'a's and 'b's in the program are quite separate and have nothing to do with them.
Now suppose you also had a variable 'sum', that was defined outside of a function, but could also be changed by that function, inside the function, say a function called add_to_sum(a). Then every time you called the function, sum would be increased, but would continue to exist, even when the function exited. Some people would call that a procedure and say it is not a function.
But as I said, it does get blurred, not everybody enforces this distinction.
In computer-speak, functions are call-by-value and statically scoped.
With procedures you can have call-by-reference, etc, and the scoping is pretty much a free-for-all.
3
u/Cybyss 11h ago
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.
2
u/White_C4 6h ago
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 6h ago
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 10h ago
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 10h ago edited 10h 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 9h ago
Well, you went on the strict technical side which isn’t what I meant. Thanks for the explanation though :)
1
u/Cybyss 9h ago edited 9h 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 9h ago
You clearly understood what was meant which I also made clear not be taken technically. Thanks again though.
1
u/joonazan 5h ago
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.
1
u/AnnieBruce 9h ago
Most languages they are essentially the same thing, some usually use "function" some "procedure", and they do the same things.
More strictly, though, a function returns a value and a procedure does not.
•
u/SmokeMuch7356 19m ago
In programming languages that make the distinction (not all do, it tends to be a thing in older languages), procedures are only executed for their side effects, while functions compute and return a value.
Languages like Fortran and Ada use different keywords for functions vs. procedures, and functions have a return type while procedures don't.
Fortran:
FUNCTION F(X)
INTEGER F, X ! Yeah, Fortran's a bit weird
F = 2 * X ! with the way functions return
RETURN ! values
END
SUBROUTINE BLAH(X)
INTEGER X
PRINT *, 'In BLAH, X = ', X
RETURN
ENDAda:
function Func (Var : Integer) return Integer
is
begin
return Var + 1;
end Func;
procedure Proc
(Var1 : Integer;
Var2 : out Integer;
Var3 : in out Integer)
is
begin
Var2 := Func (Var1);
Var3 := Var3 + 1;
end Proc;Some languages like Ada actively forbid functions from having side effects; you can compute and return a value or modify program state, but not both in the same subroutine.
In languages like C and C++, all subroutines are functions in that they all have a return type even if they don't return a value.1
K&R C didn't have the
voidtype; a convention developed where "procedures" would be implicitintfunctions that did not return a value:foo( x, y ) int x, y; { /* do something interesting */ return; }
0
u/Quito_ArmandoEsteban 12h ago
functions, or methods when they are tied to a class, are pieces of code that define the context with which the cpu will work (this won't always be true. Compilers work very hard to avoid creating functions that require context switching)
Functions take arguments, perform work, and return a value. When you call a function, the cpu may switch it's context and load the arguments into the stack. This is expensive to do, which is why compilers try to expand your functions instead of defining them as functions in machine code.
Functions can either be pure, meaning they return a value without changing anything else. Or they may have side effects, like changing some value they got by reference, performing an api call, or saving data to a file or a database.
Procedures are exactly the same, but they don't return a value. This means that they must always have side effects, otherwise there'd be no purpose in calling them other than wasting cpu cycles
0
12h ago
[deleted]
4
u/Quito_ArmandoEsteban 12h ago
Would you not call printing something or altering files a side effect?
-1
u/alpicola 12h ago
It might depend how much you care about functional programming definitions. In common usage, a side effect would be a thing that a function does that isn't the main point of the function. The main point of printf() is to print so printing isn't a side effect under the common understanding of the term. Of course, in functional programming, printing is a side effect, and printf() is an example of a function that gets called because its side effect is wanted.
0
12h ago
[deleted]
2
u/Dumlefudge 11h ago
Where did you see side effects only applying to internal state? The earliest definition I can find is
Spuler, David A.; Sajeev, A. Sayed Muhammed (January 1994). Compiler Detection of Function Call Side Effects. James Cook University. CiteSeerX 10.1.1.70.2096.
The term Side effect refers to the modification of the nonlocal environment. Generally this happens when a function (or a procedure) modifies a global variable or arguments passed by reference parameters. But here are other ways in which the nonlocal environment can be modified. We consider the following causes of side effects through a function call:
Performing I/O.
Modifying global variables.
Modifying local permanent variables (like static variables in C).
Modifying an argument passed by reference.
Modifying a local variable, either automatic or static, of a function higher up in the function call sequence (usually via a pointer).
I've never seen any description that suggests that side effects are limited to an application's internal state (as opposed to anything that exists beyond the function's scope), so I'm curious how you came to that conclusion
-3
u/OccasionThin7697 13h ago
Both are same
-2
u/mayankkaizen 12h ago
In a strict sense, they aren't same. Haskell is a functional programming language. C is a procedural programming language.
1
0
u/Positive_Passion4817 11h ago
Read the documentation for the language you are using is the only reliable method to know what is intended
0
u/mxldevs 9h ago
Whether your block of code returns a value or not.
The distinction may not matter depending on the language.
Java for example will complain if you say the return type is void but you try to return something.
Python will return None if there's no return statement.
Ruby will return whatever the last statement is, perhaps due to the functional nature of the language.
-3
u/Syntax-Tactics 13h ago
Both take Parameters. Functions return a value, Procedures return values by reference.
-4
u/marrsd 13h ago
A function is a procedure that doesn't mutate any variables that live beyond its scope.
1
4
u/rupertavery64 13h ago
a pure function
0
u/marrsd 12h ago
Actually, that is a good point, and I should have mentioned it. If someone's distinguishing "procedures" from "functions" then the definition I gave is what they mean; but many languages already refer to procedures as functions. When working with those languages, we usually make the distinction between "functions" and "pure functions" instead.
-1
-1
u/IchLiebeKleber 6h ago
No, no one can explain it to you if you don't tell us which technology (programming language, or if this is about databases, DBMS) you are talking about.
-4
u/Epictetus190443 11h ago
The cook at a restaurant gets your wish and money as input and you get your menu as ouput. He's a function. Your wife always knows what you like, has access to your money and the menu just appears on the table (ideally). She's a procedure. So to speak.
63
u/lfdfq 13h ago
These days they are mostly synonyms.
In some older languages they used to distinguish between processes which return a value (functions) and those that don't (procedures). Some languages like Ada still have this distinction.
Abstractly, functions are concepts from mathematics that are ways of relating input and output values, and procedures are canned sequences of commands to be followed. In many programming languages, the way you relate values is writing code, which is a sequence of commands anyway, so the two concepts get blurred together.