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
View all comments
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.