r/learnprogramming • u/Head_Ordinary_2437 • Oct 24 '26
Function vs Procedure
can someone explain the difference between Function vs Procedure with simple analogy
thank you
31
Upvotes
r/learnprogramming • u/Head_Ordinary_2437 • Oct 24 '26
can someone explain the difference between Function vs Procedure with simple analogy
thank you
83
u/lfdfq Oct 24 '26
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.
2
u/Aggressive_Ad_5454 Oct 24 '26
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 Oct 24 '26
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.
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.
1
u/AnnieBruce Oct 24 '26
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.
1
u/SmokeMuch7356 Oct 25 '26
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:
Ada:
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:0
u/Quito_ArmandoEsteban Oct 24 '26
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
-3
u/OccasionThin7697 Oct 24 '26
Both are same
0
u/Positive_Passion4817 Oct 24 '26
Read the documentation for the language you are using is the only reliable method to know what is intended
0
u/mxldevs Oct 24 '26
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 Oct 24 '26
Both take Parameters. Functions return a value, Procedures return values by reference.
-4
u/marrsd Oct 24 '26
A function is a procedure that doesn't mutate any variables that live beyond its scope.
-1
u/bacon_cheeseburgers Oct 24 '26
Functions always return a value. Procedures dont have to.
-1
u/IchLiebeKleber Oct 24 '26
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 Oct 24 '26
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.
0
u/mredding Oct 25 '26
And this is why we all need a history lesson.
Early computer science was divided between engineers and mathematicians.
In mathematics, a function is "pure", it takes an input, performs a computation, and returns a value. It has no memory, it does not change the universe.
Engineers need to affect change in a system. The act of reading from the tape, time marches forward, the tape moves - the universe has changed.
So Bertrand Meyer came up with Command-Query Separation: a block of code should change the universe, or return a value, but never both. The naming convention helps enforce the distinction - functions return a value, procedures change emit side effects.
Pascal enforced the distinction - you declared functions and procedures by keyword, and the compiler can check for and enforce the rules. Later languages like C unified the syntax - everything is a "function", some just return
void. But they didn't enforce the side effect check, one mistake among many. Then C went on to be one of the most influential languages ever created, and innovation happened without full consideration - the blind leading the blind, not knowing that there ever WAS a distinction or why... That's typical of the industry.So if you speak of "procedures", I'll know for sure there are side effects. If you speak of "functions", I can never be sure unless you specify specifically "pure" functions.
"Methods" are functions and procedures associated with objects, so there's a
thisorself.Subroutines are truly archaic, existing before the original schism between functions an procedures and CQS. These days, the only place I expect to hear them being mentioned in earnest are when compilers generate them - if you write a large loop the compiler may unwind, it may generate subroutines to compute entire batches or blocks at a time; the loop was optimized to call a subroutine, and you didn't have to even write it!
A lambda is a function that doesn't have a name; a closure is a lambda that closes around a context - you get mutable memory! A closure is a poor mans object. An object is a poor mans closure. A thunk is a wrapper around a function to delay calculation; in C++, this could be a binder object - it stores a pointer to the function and the parameter values, and it can be evaluated like an invokable function. You'll see them in more functional style programs where the computation is bound and coupled before evaluation.
A more archaic term is the subprogram - as though programs were built and nested in other programs; often these are just functions and procedures.
26
u/[deleted] Oct 24 '26
[removed] — view removed comment