Function (computer programming): Difference between revisions

Content deleted Content added
History: Removed citation b/c the page in the book (155) doesn't support the assertion. In fact, page 155 is the references page for chapter 10.
m clean up, typo(s) fixed: exmaple → example
Line 11:
|publisher=NIST
|quote=Callable unit: (Of a software program or logical design) Function, method, operation, subroutine, procedure, or analogous structural unit that appears within a module.
|access-date=9 February 2024}}</ref> that has a well-defined [[behavior]] and can be invoked by other software units to exhibit that behavior.
 
Callable units provide a powerful programming tool.<ref name="knuth1">{{cite book |title= The Art of Computer Programming, Volume I: Fundamental Algorithms |author= Donald E. Knuth |year= 1997 |author-link= Donald Knuth |publisher= Addison-Wesley |isbn=0-201-89683-4}}</ref> The primary purpose is to allow for the decomposition of a large and/or complicated problem into chunks that have relatively low [[cognitive load]] and to assign the chunks meaningful names (unless they are anonymous). Judicious application can reduce the cost of developing and maintaining software, while increasing its quality and reliability.<ref name="structprog">{{cite book |author= O.-J. Dahl |author2=E. W. Dijkstra |author3=C. A. R. Hoare |title= Structured Programming |publisher= Academic Press |year= 1972 |isbn= 0-12-200550-3}}</ref>
Line 29:
In January 1947 John Mauchly presented general notes at 'A Symposium of Large Scale Digital Calculating Machinery'
under the joint sponsorship of Harvard University and the Bureau of Ordnance, United States Navy. Here he discusses serial and parallel operation suggesting
{{block quoteblockquote|...the structure of the machine need not be complicated one bit. It is possible, since all the logical characteristics essential to this procedure are available, to evolve a coding instruction for placing the subroutines in the memory at places known to the machine, and in such a way that they may easily be called into use.{{pb}}In other words, one can designate subroutine A as division and subroutine B as complex multiplication and subroutine C as the evaluation of a standard error of a sequence of numbers, and so on through the list of subroutines needed for a particular problem. ... All these subroutines will then be stored in the machine, and all one needs to do is make a brief reference to them by number, as they are indicated in the coding.<ref name=mauchly0/>}}
 
[[Kathleen Antonelli|Kay McNulty]] had worked closely with John Mauchly on the [[ENIAC]] team and developed an idea for subroutines for the [[ENIAC]] computer she was programming during World War II.<ref name=":0">{{Cite web|url=http://fortune.com/2014/09/18/walter-isaacson-the-women-of-eniac/|title=Walter Isaacson on the Women of ENIAC|last=Isaacson|first=Walter|date=18 September 2014|website=Fortune|language=en|archive-url=https://web.archive.org/web/20181212003245/http://fortune.com/2014/09/18/walter-isaacson-the-women-of-eniac/|archive-date=12 December 2018|access-date=2018-12-14}}</ref> She and the other ENIAC programmers used the subroutines to help calculate missile trajectories.<ref name=":0" />
Line 44:
| access-date = February 8, 2024
}}
</ref> (1961) is one of the first computers to store subroutine return data on a stack.
 
The DEC [[PDP-6]]<ref>{{cite book
Line 151:
The features of [[implementation]]s of callable units evolved over time and varies by context.
This section describes features of the various common implementations.
 
=== General characteristics ===
 
Line 177 ⟶ 178:
=== Call syntax ===
 
If declared to return a value, a call can be embedded in an [[expression (programming)|expression]] in order to consume the return value. For example, a square root callable unit might be called like <code>y = sqrt(x)</code>.
 
A callable unit that does not return a value is called as a stand-alone [[statement (computer science)|statement]] like <code>print("hello")</code>. This syntax can also be used for a callable unit that returns a value, but the return value will be ignored.
Line 208 ⟶ 209:
In some languages, such as BASIC, a callable has different syntax (i.e. keyword) for a callable that returns a value vs. one that does not.
In other languages, the syntax is the same regardless.
In some of these languages an extra keyword is used to declare no return value; for exmapleexample <code>void</code> in C, C++ and C#.
In some languages, such as Python, the difference is whether the body contains a return statement with a value, and a particular callable may return with or without a value based on control flow.
 
Line 218 ⟶ 219:
 
{{Anchor|LVRR}} <!--what is this?-->
 
=== Local variables ===
 
Line 224 ⟶ 226:
=== Nested call {{endash}} recursion ===
 
If supported by the language, a callable may call itself, causing its execution to suspend while another ''nested'' execution of the same callable executes. [[Recursion]] is a useful means to simplify some complex algorithms and break down complex problems. Recursive languages provide a new copy of local variables on each call. If the programmer desires the recursive callable to use the same variables instead of using locals, they typically declare them in a shared context such static or global.
 
Languages going back to [[ALGOL]], [[PL/I]] and [[C (programming language)|C]] and modern languages, almost invariably use a call stack, usually supported by the instruction sets to provide an activation record for each call. That way, a nested call can modify its local variables without effecting any of the suspended calls variables.
Line 352 ⟶ 354:
 
* [[Decomposition (computer science)|Decomposing]] a complex programming task into simpler steps: this is one of the two main tools of [[structured programming]], along with [[data structure]]s
 
* Reducing [[duplicate code]] within a program
 
* Enabling [[Code reuse|reuse of code]] across multiple programs
 
* Dividing a large programming task among various programmers or various stages of a project
 
* [[Information hiding|Hiding implementation details]] from users of the function
 
* Improving readability of code by replacing a block of code with a function call where a [https://books.google.com/books?id=_i6bDeoCQzsC&pg=PA39&dq=descriptive+function+name descriptive] function name serves to describe the block of code. This makes the calling code concise and readable even if the function is not meant to be reused.
 
* Improving [[Traceability#Software|traceability]] (i.e. most languages offer ways to obtain the call trace which includes the names of the involved functions and perhaps even more information such as file names and line numbers); by not decomposing the code into functions, debugging would be severely impaired
 
Line 373 ⟶ 369:
=== Conventions ===
 
Many programming conventions have been developed regarding callables.
 
With respect to naming, many developers name a callable with a phrase starting with a [[verb]] when it does a certain task, with an [[adjective]] when it makes an inquiry, and with a [[noun]] when it is used to substitute variables.
Line 379 ⟶ 375:
Some programmers suggest that a callable should perform exactly one task, and if it performs more than one task, it should be split up into multiple callables. They argue that callables are key components in [[software maintenance]], and their roles in the program must remain distinct.
 
Proponents of [[modular programming]] advocate that each callable should have minimal dependency on the rest of the [[codebase]]. For example, the use of [[global variable]]s is generally deemed unwise, because it adds coupling between all callables that use the global variables. If such coupling is not necessary, they advise to [[code refactoring|refactor]] callables to accept passed [[parameter (computer programming)|parameters]] instead.
 
== Examples ==