2
$\begingroup$

In general, what is the difference between a function and a procedure when talking about programming languages?

$\endgroup$
4
$\begingroup$

This is rather old distincion (from pascal - which is the most recognisable for this - see vonbrand's answer).
Function returns value, procedure does not.
This is only naming convention, the more common is just calling the both function, and if needed say that one does not return value.
Some programming languages always return value (like the last statement is sent) or get returning cell on stack and does not use it.
Other languages explicitly return empty value, so still you can assign it to variable, so there is no point of this distinction.

$\endgroup$
4
$\begingroup$

This dates at least from FORTRAN (which had FUNCTIONs, returning values, and PROCEDUREs, called for their side effects). Later languages (like Pascal and descendants) kept the distinction. Pascal is probably the best known language with the distinction.

The "just call both function" common today comes from ubiquitous C, which originally (K&R, as described in Kernighan and Ritchie's "The C Programming Language", first edition) had just functions (returning ìnt by default). You could (and still can) just ignore the function's return value (really, the result of any expression), and you could just fall through the end (and return gibberish) in a function, giving the functionality of procedures without extra syntax.

Today's more strict C (and C++) still have only functions, but a function can return void (i.e., nothing), making it a procedure. In today's C, in a function returning a value a return has to give a value to return, and just falling off the end of the function isn't valid. If the function returns void, any return has to be bare (no value to return) and falling off the end (no return, for example) is legal.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.