Hướng dẫn procedure vs function python
This is a well-known old question, but I’d like to share some more insights about modern programming language research and design. Show Nội dung chính
Basic answer
Traditionally (in the sense of structured programming) and informally, a procedure is a reusable structural construct to have “input” and to do something programmable. When
something is needed to be A function is more than a procedure because return values can also be specified as the “output” in the body.
Function calls are more or less Traditionally, procedure calls (rather than function calls) are used to indicate that no output must be interested, and there must be side effects to avoid the call being no-ops, hence emphasizing the imperative programming paradigm. Many traditional programming languages like Pascal provide both
“procedures” and “functions” to (To be clear, the “input” and “output” mentioned above are simplified notions based on the syntactic properties of functions. Many languages additionally support passing arguments to parameters by reference/sharing, to allow users transporting information encoded in arguments during the calls. Such parameter may even be just called as “in/out parameter”. This feature is based on the nature of the objects
being passed in the calls, which is However, if the result of a function call is not needed, it can be just (at least logically) ignored, and function definitions/function calls should be consistent to procedure definitions/procedure calls in this way. ALGOL-like languages like C, C++ and Java, all provide the feature of “function” in this fashion: by encoding the result type Since SICP is mentioned, it is also worth noting that in the Scheme language specified by RnRS, a procedure may or may not have to return the result of the computation. This is the union of the traditional “function” (returning the result) and “procedure” (returning nothing), essentially same to the “function” concept of many
ALGOL-like languages (and actually sharing even more I don’t know much about the exact reasons behind the divergence, but as I have experienced, it seems that language designers will be happier without specification bloat nowadays. That is, “procedure” as a standalone feature is unnecessary. Techniques like In theory, a function can be specified with a specified unit type as the type of the function call result to indicate that result is special. This distinguishes the
traditional procedures (where the result of a call is uninterested) from
Further readingAs the traditional concept derived from math, there are tons of black magic most people do not bother to know. Strictly speaking, you won’t be likely get the whole things clear as per your math books. CS books might not provide much help, either. With concerning of programming languages,
What is the main difference between a function and a stored procedure?The function What is the relationship between a function and a procedure?A procedure is a routine that can accept |