close
Jump to content

Talk:Function (computer programming)

Page contents not supported in other languages.
From Wikipedia, the free encyclopedia


The Python examples is riddled with mistakes

[edit]

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.

def simple_function():
    print('Hello world!')
    print('Wikipedia')
simple_function()
# output will be:
Hello World!
Wikipedia
                  

def func(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]

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

Procedure context

[edit]

In languages such as Algol 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 a Closure. I'm not quite sure where this belongs, but I believe that the article should discuss it in an NPOV 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 function in 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]
Shmuel (Seymour J.) Metz Username:Chatul, While I agree with Stevebroshar that this article shouldn't try to cover every feature of every function-like thing in every programming language, I feel that most of them should be mentioned *somewhere* in Wikipedia, and ideally this article would link (directly or indirectly) to those function-like things. I see this article *already* links to Nested function, Closure (computer programming), and Method (computer programming). Perhaps this article is the best place for a brief comparison discussing the differences between them. --DavidCary (talk) 00:40, 21 November 2025 (UTC)[reply]

All the names

[edit]

@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 uses callable unit but it's not generally accepted. It's used as a way to refer to the more general concept. The article is named function even though it's _not_ the most general term. It just happens to be very commonly used today. The article used to be called subprogram.

IMO and due to historical reasons it seems valuable to leave in Subprogram was used long ago but today is antiquated.

WRT adding subprogram 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. Seems subprogram is related to program 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]
Who uses subprogram? Stevebroshar (talk) 10:31, 28 June 2024 (UTC)[reply]
https://www.adaic.com/resources/add_content/standards/05rm/html/RM-6-4.html
https://llvm.org/doxygen/classllvm_1_1DISubprogram.html
https://www.ibm.com/docs/en/xffbg/121.141?topic=procedures-function-subroutine-subprograms
Admittedly, not many. Meownium (talk) 01:42, 29 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]

Incorrect language tag

[edit]

@Cedar101: Edit permalink/1318989262 added <syntaxhighlight lang="rexx">...</syntaxhighlight> around example code that is written in PL/I, not Rexx, with the comment →PL/I: syntaxhighlight. I reverted it, with the comment Undid revision by Cedar101 It's not Rexx and user:Cedar101 reinstated it with the comment Mike Cowlishaw, the developer of Rexx, "intended Rexx to be a simplified and easier to learn version of PL/I". See Rexx#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 statement declare array(*,*) float; in the sample code causes Error 35 Invalid expression with the error text Incorrect 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, and call that calls it, so it is reasonable to designate the language as Rexx. Cedar101 (talk) 00:23, 29 October 2025 (UTC)[reply]
It does not, however, highlight DECLARE or any of the data attributes, e.g., BINARY, DECIMAL, FIXED, FLOAT. -- Shmuel (Seymour J.) Metz Username:Chatul (talk) 12:36, 29 October 2025 (UTC)[reply]
According to Footnote 3 in the MediaWiki SyntaxHighlight extension documentation, "There is a lexer for every language or markup format that Pygments supports. See https://pygments.org/docs/quickstart/".
Pygments has no lexer for PL/I, but it might be possible to take the lever for Rexx and modify it to do PL/I, e.g. adding additional keywords, for example as a pull request on its Git repository. Guy Harris (talk) 22:30, 29 October 2025 (UTC)[reply]
The presence of pli||PL/I in the table suggests that there used to be one. If so, it's probably possible to retrieve it. -- Shmuel (Seymour J.) Metz Username:Chatul (talk) 13:48, 30 October 2025 (UTC)[reply]
If that's the table at Template:Syntaxhighlight § Supported languages, then this 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 never in 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]

Unusual semantics in some languages?

[edit]

I'm not sure of where they belong in the article, but two languages (SL5, Icon) developed by Ralph 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 for success or 0 for failure. 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 last return.

Icon, OTOH, uses backtracking.

Are there other languages using these concepts, and do they belong in the article? -- Shmuel (Seymour J.) Metz Username:Chatul (talk) 13:39, 14 November 2025 (UTC)[reply]