Function (computer programming): Difference between revisions

Content deleted Content added
Terminology: PL/I uses procedure for both
add link to main article on leaf functions
Line 140:
 
====Delayed stacking {{anchor|Leaf procedure}}{{anchor|Leaf function}}====
 
One disadvantage of the call stack mechanism is the increased cost of a procedure call and its matching return.{{ clarify | date = November 2015 | reason = increased relative to what?}} The extra cost includes incrementing and decrementing the stack pointer (and, in some architectures, checking for [[stack overflow]]), and accessing the local variables and parameters by frame-relative addresses, instead of absolute addresses. The cost may be realized in increased execution time, or increased processor complexity, or both.
 
This overhead is most obvious and objectionable in ''leaf procedures'' or ''[[leaf functionsfunction]]s'', which return without making any procedure calls themselves.<ref>{{cite web|url=http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.faqs/ka13785.html |title=ARM Information Center |publisher=Infocenter.arm.com |access-date=2013-09-29}}</ref><ref>{{cite web |title=x64 stack usage |url=https://docs.microsoft.com/en-us/cpp/build/stack-usage |website=Microsoft Docs |publisher=Microsoft |access-date=5 August 2019}}</ref><ref>{{cite web|url=http://msdn.microsoft.com/en-us/library/67fa79wz%28v=VS.90%29.aspx |title=Function Types |publisher=Msdn.microsoft.com |access-date=2013-09-29}}</ref> To reduce that overhead, many modern compilers try to delay the use of a call stack until it is really needed.{{Citation needed|date=June 2011}} For example, the call of a procedure ''P'' may store the return address and parameters of the called procedure in certain processor registers, and transfer control to the procedure's body by a simple jump. If the procedure ''P'' returns without making any other call, the call stack is not used at all. If ''P'' needs to call another procedure ''Q'', it will then use the call stack to save the contents of any registers (such as the return address) that will be needed after ''Q'' returns.<!-- Mention caller-saves vs. callee-saves?-->
<!---If the procedure or function itself uses stack handling commands, outside of the prologue and epilogue, e.g. to store intermediate calculation values, the programmer needs to keep track of the number of 'push' and 'pop' instructions so as not to corrupt the original return address.-->
<!--Major cleanup needed from this point on-->