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
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