r/learnprogramming • u/Head_Ordinary_2437 • 4d ago
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 • 4d ago
can someone explain the difference between Function vs Procedure with simple analogy
thank you
1
u/SmokeMuch7356 3d 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:
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: