This article is within the scope ofWikiProject Computer science, a collaborative effort to improve the coverage ofComputer science related articles on Wikipedia. If you would like to participate, please visit the project page, where you can jointhe discussion and see a list of open tasks.Computer scienceWikipedia:WikiProject Computer scienceTemplate:WikiProject Computer scienceComputer science
If that is intentional it should be pointed out in the text, but as of writing this, it only says "The following program prints "Hello world!" followed by "Wikipedia" on the next line.
defsimple_function():print('Hello world!')print('Wikipedia')simple_function()# output will be:HelloWorld!Wikipediadeffunc(name)print("welcome "+name)print(func("martin"))#output will be: welcome martin
If you were to try and run this with Python, lines 6 and 7 would lead to syntax errors because they are not commented. The second example function is worse, the def statement does not end with a colon leading to another syntax error and because the function does not return anything, printing the return value of the function will print "None" after printing "welcome martin"2001:7C0:2049:1D4:D511:6438:FDB6:39FC (talk)14:33, 22 November 2023 (UTC)[reply]
Done. All the mistakes you pointed out have been fixed. The current version works as described when I copy-and-paste into my Python3 interpreter. --DavidCary (talk)23:57, 6 October 2025 (UTC)[reply]
This page has ALOT of history – is old with lots of edits. One could write an article about the history of this article :) I am new to this article, but have been programming for 30 years. I'm interested in improving this article – mostly for up and coming programmers wanting to learn the trade. As it was when I started working on it a few says ago, it was a mess – filled with inconsistent use of terms and wrong/bad info.
It tries to cover alot of ground – probably too much. But I'm focused on correcting what's there more than removing what's too far afield. At some point, maybe I'll get to refactoring into other pages.
Probably the biggest challenge for this article is scope. What is this about? Its title is 'function', but clearly it's also about subprogram ... and procedure ... and method ... OMG. Thing is, these terms all have meaning in the context of certain languages and environments. Even within particular contents, the meaning has changed over time -- over the last 70 years ... in arguably the most dynamic industry in human evolution! This article takes on a broad and deep topic.
But, I think it's worthwhile. Since whatever this article is about is clearly important.
But, to be useful and understandable, the article needs to be focused – as much as possible for such a board topic.
IMO this article always has been and should continue to be about thetechnology (or concept or construct) that is common among all languages and environments that allows programmers to structure code into chucks that can be used within a program to decompose a problem into a solution.
One challenge of this is that there is no one, good word for that!
Seems thatfunction has connotations of the technology in general, but it is also bound tightly to particular implementations – contexts such as particular languages. Using it for the name of the technology seems off the mark.
Some say thatsubprogram is the general concept. In fact, I think this article used to be calledsubprogram. Why was it changed? Was the change a mistake?
An issue that I personally struggle with is that I often confusesubprogram andsubroutine even though they are quite different, right? Subroutine is like function – an implementation of subprogram, right?
The article does use the termcallable unit which does seem to cover the desired focus of this article. Unfortunately, it's not commonly used – not nearly as commonly used as the other terms (function, subprogram, ...). But, a Google search does have hits (other than this article). So, it is a thing apparently.
I think this article should be renamed – either back tosubprogram or tocallable unit. And, content should be updated to use the new name throughout when referring to the general concept. And, to continue to use the more specific terms as context warrants.
This article already has re-directs for all of the more specific terms and that should remain. Folks do and should continue to come to this article for all the various terms (callable unit, function, subprogram, ...).
...Just reviewed the edit history of this page. Seems that the page rename fromsubprogram to currentfunction was done by Uni3993 in late 2022. And they seem to be permanently blocked :o) From that I take it that they have a history of over zealous modifications. It seems they did a global search and replace of 'subprogram' with 'function'. Well intentioned, but badly implemented. For one, when the word started a sentence, it was no longer capitalized. For another, I guess the result had "function and function". OMG. What a mess. Guy Harris fixed the many, many issues. Thanks for that!! I do want move away from 'function' as the general term, but do _not_ propose a simple minded search and replace. Would require a surgical approach.
In languages such asAlgol 60 and PL/I, it is possible to define a procedure within another procedure. The procedure name identifies not only an entry point but also a context (stack frame) for local variables of the enclosing block. Similarly, in object oriented languages, an instance method is assciated with the context of an object, not just with its class.This is a special case of aClosure. I'm not quite sure where this belongs, but I believe that the article should discuss it in anNPOV manner and then give examples. --Shmuel (Seymour J.) Metz Username:Chatul (talk)19:43, 8 February 2024 (UTC). -- Revised 16:32, 12 February 2024 (UTC)[reply]
I don't think this article should attempt to describe every feature of a functionin every language. I like that it tries to cover many aspects of functions (of whatever we call them) with examples from various languages. But, if we try to cover every feature for every language, the article will be too long ... and boring :) If it's wrong about algol and pl/l we should fix it. But IMHO we don't need to be exhaustive about each language.Stevebroshar (talk)00:23, 13 February 2024 (UTC)[reply]
I was thinking that the "a recursive callable must be reentrant" was not right. Please fix that section. Thanks. ... but I don't know what you mean by "A recursive function can fail to be reentrant due to unserialized access to global data". Try to keep it high level. This is not for compiler designers. I'd say it's for high school and college students learning to program.Stevebroshar (talk)00:27, 13 February 2024 (UTC)[reply]
Fixed.
Consider a factorial function
FACT: PROCEDURE(N) RECURSIVE RETURNS(FIXED BINARY);DECLARE COUNT FIXED BINARY EXTERNAL INIT(0) N FIXED BINARY; COUNT=COUNT+1; /* Not protected by a semaphor]] SELECT; WHEN (COUNT=0) RETURN 1; WHEN (COUNT>0) RETURN N*FACT(N-1); OTHERWISE SIGNAL CONDITION(NEGATIVE); END; END;
Removal of "[callable unit] is not commonly used" is not wrong ... but...
Now it only says "The technology is generically called acallable unit" ... which is not wrong, ... but...
I am very concerned about how information can be misleading. To _not_ say that it's not common leads the reader to think 'callable unit' is common -- otherwise it wouldn't be in wikipedia, right? But, it is uncommon.
I struggle with what to call the thing that is the focus of this article. 'function' is not great since so many other terms have been used throughout the years. Even today 'function' is not the only term used. I would love to have a term that covers it all. But, there just isn't acommon term that covers the breadth of the topic. 'callable unit' does, but it's not common and that fact is important IMHO.
It's like what is the gender neutral, singular pronoun? There isn't one!
It's okay to start the history with "subroutine" and make that the most generic term. Then as technology improves, "procedures", "functions", and "methods" can be added to the vocabulary. Rather than trying to figure this out yourself, I suggest getting one of these books:Programming_language#Further_reading. I haveComparative Programming Languages by Wilson. There's a chapter called "Procedures, functions and methods" that is ripe for this article. This article also needs an explanation of the different parameter methods. Additionally, this article only has a superficial coverage of how functions are implemented.Andrew S. Tanenbaum is the godfather of modern computer hardware. I have the 1990 edition of his seminal book,Structured Computer Organization. (Subsequent editions are out there.) In this book, stack addressing and the stack frame are well covered.Timhowardriley (talk)03:14, 13 February 2024 (UTC)[reply]
I think my father had books :) Joking aside, I think you are right that historically subroutine ... and subprogram ... and routine ... are older terms, then function came along to more or less replace them and then method came with OOP but doesn't replace function. That's my hypothesis. Is there a source that covers this aspect of history of this thing with many names? ... Comparative Programming Languages costs $3! I didn't plan on investing so much as part of my wikipedia hobby.Stevebroshar (talk)11:26, 15 February 2024 (UTC)[reply]
Functions are overly restrictive. What about relations? And whysequences of instructions? Why excludesets ofcallable units, which can be called in the order in which they are needed, or inparallel.
In the family of Prolog and Dataloglogic programming languages, acallable unit is a predicate representing a relation. Moreover, a predicate can be defined in terms of other predicates, and can either becalled using top-downbackward reasoning, as in Prolog, or bederived bottom-up usingforward reasoning, as in most Datalog systems.
What do you mean by "Functions are overly restrictive"? That the termfunction is overly restrictive WRT the concept that the page is intended to cover? You think the central concept of the page isrelation? If that's what you mean, then no, that's not what this article is about. ... unless I don't know what you mean by relation. I think of relation as a static association between things. Whereas a function is a dynamic action. Very different.
I think it's appropriate to call a function a sequence of instructions since the text of a function is a sequence of lines. Control flow when executed may not be sequential -- one line to the next -- although that is the default control flow when not otherwise directed by the instructions.
WRT "Why exclude sets of callable units": in what way are they excluded? You mean that concept should be included? Isn't that basically a library ... which is described?
... oh you are thinking about Prolog and Datalog logic programming (youburied the lede). OK. I don't know much about that. But, I wonder whether this predicate concept falls under the concept of this article (function...). Maybe the concept of this article only applies to imperative languages -- not other languages such as logic programming. Is a predicate something that gets called per se? Or is it a higher level concept? I'm sure a logic languageprocessor (compiler/interpreter) includes callables. At the bottom level of abstraction, all programs execute as imperative instructions and almost surely include callables. But does it make sense to say that from the perspective of logic language source code to talk about calls? I'm thinking not.Stevebroshar (talk)11:58, 15 February 2024 (UTC)[reply]
To put it differently,functions are not the only way to "structure code into chucks that can be used within a program to decompose a problem into a solution". Relations are more general than functions and can also be used to stucturecode. I am certainly not suggesting that the article be renamedRelation (computer programming).Callable unit is not great either. I can live withsubprogram, butfunction is simply not the topic of most of the article.Robert Kowalski (talk)13:08, 15 February 2024 (UTC)[reply]
It can be challenging to know the intent of something, But, based on various factors I think this article is about the calling technology; not chunking. For example, a graphical language allows chunking via visual things; shapes or something. That is chunking, but clearly not what this article is about. I also don't think this is about a more general concept of chunking. The callable nature seems to be central to this article. Notice how it gets into gory details of call/return, call stack and such. This is about calling ... which today implies chunking ... although in some older languages it didn't involve chunking. ... Including a short bit on chunking seems like a good addition since it is part of the modern concept of a callable. ... I do think the article is about the concept offunction, but only in a modern sense of the word. Way back, other terms were used, but nowfunction is a common term to use for anything that is callable. It is an overloaded word for sure. Function means lots of things. Further, terms like subprogram, subroutine, routine are not common today although used to be.Stevebroshar (talk)11:06, 17 February 2024 (UTC)[reply]
Many thanks for this clarification, @Stevebroshar. I understand you as saying that in this article the termfunction is not used in the sense offunctional programming or in the sense of mathematicalfunction, but in some broader sense that includes the notion offunction in functional programming. In particular, it includes, for example, the use of predicates in the procedural interpretation of logic programs. If so, then maybe it would be good to add something along these lines to the article. As it currently stands, the topic of the article may be easily misunderstood.Robert Kowalski (talk)09:57, 18 February 2024 (UTC)[reply]
"What something does or is used for. Synonyms: aim, intention, purpose, role, use."
The sixth and seventh definitions are
"(mathematics) A relation in which each element of the domain is associated with exactly one element of the codomain. Synonyms: map, mapping, mathematical function, operator, transformation
(computing) A routine that receives zero or more arguments and may return a result. Synonyms: procedure, routine, subprogram, subroutine, func, funct"
Yes. #7. Interesting that it starts with 'routine' which seems to be one of the rarest used synonyms. But, that definition is just what this article is about: a thing that has all those names. A thing that does not have just one name that is commonly used and separate from other words. The term 'function' is correct, but it ishighly overloaded which of course leads to confusion. This page used to be named 'subroutine', but was renamed in 2022 or something. I think that change was a not good. I also think that 'subroutine' is not a good name since it is so rarely used today. ... This article is about the mechanics of calling -- a technology that probably is much older than the concept offunctional programming. The technology surely is used in functional programming, but at a lower level of abstraction. ... Maybe this article should be called 'Function call'.Stevebroshar (talk)08:10, 19 February 2024 (UTC)[reply]
I like it. ... even though I think it's inappropriate or something like that :( I did find that NIST reference forcallable unit, but... it's not a common term and I think that's relevant. I wish it was common, but my wishing does not make it so. It's an obscure reference that happens to be from a non-obscure publisher. I'm not going to change it though. Maybe this reference will lead to its integration into the lexicon; something wikipedia is not supposed to do ;)Stevebroshar (talk)14:49, 23 February 2024 (UTC)[reply]
In Mathematics, it is common for a text to begin with a discussion of nomenclature, explicitly steting which of several different conventions is used. IMHO, doing the same thing in Wikipedia articles would be desirable. It doesn't much matter whether the article uses the termsextenal unit,procedure orroutine, as long as it notes the existence of alternative nomenclature and is consistent.
Another concept that requires clear nomenclature is the components of a callable unit. Usingline is misleading, since a line of source code may contain multiple statement and conversely a statement may spread over several lines. This applies equally to functional and procedural languages. I suspect that it also applies to rule-based languages. --Shmuel (Seymour J.) Metz Username:Chatul (talk)14:26, 16 February 2024 (UTC)[reply]
About 'line': I agree that line can be misleading since some languages allow multiple statements on a line. But ... some don't. I think BASIC does not so talking about lines is OK. Could use statement with BASIC I guess. I'm curious to read the part(s) with 'line' that you think are misleading.Stevebroshar (talk)11:12, 17 February 2024 (UTC)[reply]
^"Terminology Glossary".nist.gov. NIST. Retrieved9 February 2024.Callable unit: (Of a software program or logical design) Function, method, operation, subroutine, procedure, or analogous structural unit that appears within a module.
@Meownium This article is a PITA due to all the different names that have different meaning, but all refer to something that is so very core to programming ... that itself does not have a general name. The article usescallable unit but it's not generally accepted. It's used as a way to refer to the more general concept. The article is namedfunction even though it's _not_ the most general term. It just happens to be very commonly used today. The article used to be calledsubprogram.
IMO and due to historical reasons it seems valuable to leave inSubprogram was used long ago but today is antiquated.
WRT addingsubprogram to the part about routine/subroutine being like [sub]directory I think is inaccurate. It seems routine and subroutine are a pair like directory and subdirectory. Seems subprogram is not related to [sub]routine in that way. Seemssubprogram is related toprogram in that same way.Stevebroshar (talk)18:32, 26 June 2024 (UTC)[reply]
I don't think it's perfectly valid to call "subprogram" an antiquated term as it's still used (though, not as frequently as the other terms featured in the article); but your assessment on roping it in with subroutine/routine is definitely true. I'll go ahead and revise, thank you for the input!Meownium (talk)00:35, 27 June 2024 (UTC)[reply]
I edited it again since there were multiple grammatical errors and overly wordy. Concise is good. I've tried to keep all concepts that you added.Stevebroshar (talk)13:39, 28 June 2024 (UTC)[reply]
The 2nd sentence of that article: "All generators are also iterators." so same deal. Routines are ubiquitous and used in implementing many things, including these constructs. -R. S. Shaw (talk)18:42, 28 June 2024 (UTC)[reply]
@Cedar101: Editpermalink/1318989262 added<syntaxhighlight lang="rexx">...</syntaxhighlight> around example code that is written inPL/I, notRexx, with the comment→PL/I: syntaxhighlight. I reverted it, with the commentUndid revision by Cedar101 It's not Rexx anduser:Cedar101 reinstated it with the commentMike Cowlishaw, the developer of Rexx, "intended Rexx to be a simplified and easier to learn version of PL/I". SeeRexx#History. That last comment, while correct, is irrelevant; PL/I syntax is not Rexx syntax, nor did Dr. Cowlishaw ever claim that it was. In fact, the statementdeclare array(*,*) float; in the sample code causes Error 35 Invalid expression with the error textIncorrect expression detected at "*". --Shmuel (Seymour J.) Metz Username:Chatul (talk)08:51, 28 October 2025 (UTC)[reply]
In most Wikipedia articles, if there is a language that<syntaxhighlight> does not support, another language with similar grammar is often specified. In that section, it is important to emphasize comments (/* */),procedure_name:procedure...end that defines a function, andcall that calls it, so it is reasonable to designate the language as Rexx.Cedar101 (talk)00:23, 29 October 2025 (UTC)[reply]
If that's the table atTemplate:Syntaxhighlight § Supported languages, thenthis bug report suggests that it might have disappeared as a result of the switch from GeSHi to Pygments. If so, it's not that it was removed from Pygments, it's that it was neverin Pygments but was in GeSHi, so there's nothing to retrieve from Pygments, but there may be stuff in [GeSHi] that could be used when developing a Pygments implementation.Guy Harris (talk)21:04, 14 November 2025 (UTC)[reply]
I'm not sure of where they belong in the article, but two languages (SL5,Icon) developed byRalph Griswold ז״ל have function semantics somewhat different from the norm.
In the case of SL5, a function returns a result consisting of a value and a positive integer signal; they signal is typically either 1 forsuccess or 0 forfailure. Control flow in compound statements, e.g.,if,while, is driven by the signal from the control expression, and it is possible to resume a function at the statement following the lastreturn.