Return statement

In computer programming, a return statement causes execution to leave the current subroutine and resume at the point in the code immediately after the instruction which called the subroutine, known as its return address. The return address is saved by the calling routine, today usually on the process's call stack or in a register. Return statements in many programming languages allow a function to specify a return value to be passed back to the code that called the function.

Overview

In C and C++, return exp; (where exp is an expression) is a statement that tells a function to return execution of the program to the calling function, and report the value of exp. If a function has the return type void, the return statement can be used without a value, in which case the program just breaks out of the current function and returns to the calling one.[1][2] Similar syntax is used in other languages including Modula-2[3] and Python.[4]

In Pascal there is no return statement. Functions or procedures automatically return when reaching their last statement. The return value from a function is provided within the function by making an assignment to an identifier with the same name as the function.[5] However, some versions of Pascal provide a special function Exit(exp); that can be used to return a value immediately from a function, or, without parameters, to return immediately from a procedure.[6]

Like Pascal, FORTRAN II, Fortran 66, Fortran 77, and later versions of Fortran specify return values by an assignment to the function name, but also have a return statement; that statement does not specify a return value and, for a function, causes the value assigned to the function name to be returned.[5][7][8]

In some other languages a user defined output parameter is used instead of the function identifier.[9]

Oberon (Oberon-07) has a return clause instead of a return statement. The return clause is placed after the last statement of the procedure body.[10]

Some expression-oriented programming language, such as Lisp, Perl and Ruby, allow the programmer to omit an explicit return statement, specifying instead that the last evaluated expression is the return value of the subroutine. In other cases a Null value is returned if there is no explicit return statement: in Python, the value None is returned when the return statement is omitted,[4] while in JavaScript the value undefined is returned.

In Windows PowerShell all evaluated expressions which are not captured (e.g., assigned to a variable, cast to void or piped to $null) are returned from the subroutine as elements in an array, or as a single object in the case that only one object has not been captured.

In Perl, a return value or values of a subroutine can depend on the context in which it was called. The most fundamental distinction is a scalar context where the calling code expects one value, a list context where the calling code expects a list of values and a void context where the calling code doesn't expect any return value at all. A subroutine can check the context using the wantarray function. A special syntax of return without arguments is used to return an undefined value in scalar context and an empty list in list context. The scalar context can be further divided into Boolean, number, string, and various reference types contexts. Also, a context-sensitive object can be returned using a contextual return sequence, with lazy evaluation of scalar values.

Many operating systems let a program return a result (separate from normal output) when its process terminates; these values are referred to exit statuses. The amount of information that can be passed this way is quite limited, in practice often restricted to signalling success or fail. From within the program this return is typically achieved by calling Exit (system call) (common even in C, where the alternative mechanism of returning from the main function is available).

Syntax

Return statements come in many shapes. The following syntaxes are most common:

Language Return statement If value omitted, return
Ada, Bourne shell,[a] C, C++, Java, PHP, C#, JavaScript, D
return value;
In the Bourne shell, exit value of the last command executed in the function

In C[1] and C++,[2] undefined behavior if function is value-returning

In PHP,[12] returns NULL

In Javascript,[13] returns the value undefined

In Java and C#, not permitted if function is value-returning

BASIC
RETURN
Lisp
(return value)
Last statement value
Perl, Ruby
return @values;
return $value;
return;

or a contextual return sequence

Last statement value
PL/I
return(expression);
return;
Undefined behavior if procedure is declared as returning a value
Python
return value
None[4]
Smalltalk
^ value
Tcl
return
return $value
return -code error "Error message"

or some more complicated combination of options

Last statement value
Visual Basic .NET
Return value
Windows PowerShell
return value;
Object
x86 assembly
ret
Contents of eax register (by conventions)

In some assembly languages, for example that for the MOS Technology 6502, the mnemonic "RTS" (ReTurn from Subroutine) is used.

Multiple return statements

Languages with an explicit return statement create the possibility of multiple return statements in the same function. Whether or not that is a good thing is controversial.

Strong adherents of structured programming make sure each function has a single entry and a single exit (SESE). It has thus been argued[14] that one should eschew the use of the explicit return statement except at the textual end of a subroutine, considering that, when it is used to "return early", it may suffer from the same sort of problems that arise for the GOTO statement. Conversely, it can be argued that using the return statement is worthwhile when the alternative is more convoluted code, such as deeper nesting, harming readability.

In his 2004 textbook, David Watt writes that "single-entry multi-exit control flows are often desirable". Using Tennent's framework notion of sequencer, Watt uniformly describes the control flow constructs found in contemporary programming languages and attempts to explain why certain types of sequencers are preferable to others in the context of multi-exit control flows. Watt writes that unrestricted gotos (jump sequencers) are bad because the destination of the jump is not self-explanatory to the reader of a program until the reader finds and examines the actual label or address that is the target of the jump. In contrast, Watt argues that the conceptual intent of a return sequencer is clear from its own context, without having to examine its destination. Furthermore, Watt writes that a class of sequencers known as escape sequencers, defined as "sequencer that terminates execution of a textually enclosing command or procedure", encompasses both breaks from loops (including multi-level breaks) and return statements. Watt also notes that while jump sequencers (gotos) have been somewhat restricted in languages like C, where the target must be an inside the local block or an encompassing outer block, that restriction alone is not sufficient to make the intent of gotos in C self-describing and so they can still produce "spaghetti code". Watt also examines how exception sequencers differ from escape and jump sequencers; for details on this see the article on structured programming.[15]

According to empirical studies cited by Eric S. Roberts, student programmers had difficulty formulating correct solutions for several simple problems in a language like Pascal, which does not allow multiple exit points. For the problem of writing a function to linearly searching an element in an array, a 1980 study by Henry Shapiro (cited by Roberts) found that using only the Pascal-provided control structures, the correct solution was given by only 20% of the subjects, while no subject wrote incorrect code for this problem if allowed to write a return from the middle of a loop.[16]

Others, including Kent Beck and Martin Fowler argue that one or more guard clauses—conditional "early exit" return statements near the beginning of a function—often make a function easier to read than the alternative.[17][18][19][20]

The most common problem in early exit is that cleanup or final statements are not executed – for example, allocated memory is not unallocated, or open files are not closed, causing leaks. These must be done at each return site, which is brittle and can easily result in bugs. For instance, in later development, a return statement could be overlooked by a developer, and an action which should be performed at the end of a subroutine (e.g. a trace statement) might not be performed in all cases. Languages without a return statement, such as standard Pascal don't have this problem. Some languages, such as C++ and Python, employ concepts which allow actions to be performed automatically upon return (or exception throw) which mitigates some of these issues – these are often known as "try/finally" or similar. Functionality like these "finally" clauses can be implemented by a goto to the single return point of the subroutine. An alternative solution is to use the normal stack unwinding (variable deallocation) at function exit to unallocate resources, such as via destructors on local variables, or similar mechanisms such as Python's "with" statement.

Some early implementations of languages such as the original Pascal and C restricted the types that can be returned by a function (e.g. not supporting record or struct types) to simplify their compilers.

In Java—and similar languages modeled after it, like JavaScript—it is possible to execute code even after return statement, because the finally block of a try-catch structure is always executed. So if the return statement is placed somewhere within try or catch blocks the code within finally (if added) will be executed. It is even possible to alter the return value of a non primitive type (a property of an already returned object) because the exit occurs afterwards as well.[21]

Yield statements

Cousin to return statements are yield statements: where a return causes a subroutine to terminate, a yield causes a coroutine to suspend. The coroutine will later continue from where it suspended if it is called again. Coroutines are significantly more involved to implement than subroutines, and thus yield statements are less common than return statements, but they are found in a number of languages.

Call/return sequences

A number of possible call/return sequences are possible depending on the hardware instruction set, including the following:

  1. The CALL instruction pushes address of the next instruction on the stack and branches to the specified address. The RETURN instruction pops the return address from the stack into the instruction pointer and execution resumes at that address. (Examples: x86, PDP-11) In architectures such as the Motorola 96000, the stack area may be allocated in a separate address space, which is called 'Stack Memory Space',[22] distinct from the main memory address space.[23] The NEC μPD7720 also features a stack with its own separate address space.[24]
  2. The CALL instruction places address of the next instruction in a register and branches to the specified address. The RETURN instruction sequence places the return address from the register into the instruction pointer and execution resumes at that address. (Examples: IBM System/360 and successors through z/Architecture, most RISC architectures)
  3. The CALL instruction places address of the next (or current) instruction in the storage location at the call address and branches to the specified address+1. The RETURN instruction sequence branches to the return address by an indirect jump to the first instruction of the subroutine. (Examples: IBM 1130, SDS 9XX, PDP-8)

See also

Notes

  1. ^ in the Bourne shell, only integers in the range 0-255 may be returned[11]

References

  1. ^ a b "return Statement (C)". Microsoft Docs. 25 January 2023.
  2. ^ a b "return Statement (C++)". Microsoft Docs. 3 August 2021.
  3. ^ Gleaves, R. (2012). Modula-2 for Pascal Programmers. Springer. p. 71. ISBN 9781461385318.
  4. ^ a b c Martelli, Alex (2006). Python in a Nutshell: A Desktop Quick Reference (2nd ed.). O'Reilly Media. p. 73. ISBN 9781449379100.
  5. ^ a b Scott, Michael L. (2006). Programming Language Pragmatics. Morgan Kaufmann. p. 432. ISBN 9780126339512.
  6. ^ Flanders, Harley (2012). Scientific Pascal. Springer. p. 35. ISBN 9781461224280.
  7. ^ ANSI x3.9-1966. USA Standard FORTRAN (PDF). American National Standards Institute. p. 14. Archived from the original (PDF) on May 15, 2011. Retrieved May 5, 2010.{{cite book}}: CS1 maint: numeric names: authors list (link)
  8. ^ ANSI x3.9-1978. American National Standard – Programming Language FORTRAN. American National Standards Institute. 15.8 RETURN Statement. Archived from the original on October 29, 2013. Retrieved December 11, 2007.{{cite book}}: CS1 maint: numeric names: authors list (link)
  9. ^ Sakkinen, Markku (March 1989). "How to best return the value of a function". ACM SIGPLAN Notices. 24 (3). Association for Computing Machinery: 55–56. doi:10.1145/66083.66087.
  10. ^ Wirth, Niklaus (May 3, 2016). "10. Procedure declarations". The Programming Language Oberon (PDF) (Report). p. 11.
  11. ^ "return - return from a function or dot script". Single UNIX Specification.
  12. ^ "PHP: return - Manual". PHP Manual. The PHP Group. Retrieved 26 March 2013.
  13. ^ "Return - Javascript". MDN Javascript Reference. Mozilla Developer Network. Retrieved 27 March 2013.
  14. ^ "C++ Notes: Function return Statement".
  15. ^ Watt, David Anthony; Findlay, William (2004). Programming Language Design Concepts. John Wiley & Sons. pp. 215–221. ISBN 978-0-470-85320-7.
  16. ^ Roberts, E. (March 1995). "Loop Exits and Structured Programming: Reopening the Debate". ACM SIGCSE Bulletin. 27 (1): 268–272. doi:10.1145/199691.199815.
  17. ^ Martin Fowler; Kent Beck; John Brant; William Opdyke; Don Roberts (2012). Refactoring: Improving the Design of Existing Code (Google eBook). Addison-Wesley. pp. 237, 250. ISBN 9780133065268. ... one exit point mentality ... I don't follow the rule about one exit point from a method.
  18. ^ Kent Beck (2007). "7: Behavior". Implementation Patterns. Pearson Education. section "Guard Clause". ISBN 9780132702553.
  19. ^ "Multiple return statements". Java Practices.
  20. ^ Fred Swartz. "Return statements and the single exit fantasy". Archived from the original on 2020-02-23.
  21. ^ "The finally Block". The Java Tutorials.
  22. ^ "DSP96002 32-BIT DIGITAL SIGNAL PROCESSOR USER'S MANUAL" (PDF). p. 27(3 - 4). Retrieved 2023-12-24.
  23. ^ "DSP96002 32-BIT DIGITAL SIGNAL PROCESSOR USER'S MANUAL" (PDF). p. 50(4 - 11). Retrieved 2023-12-24.
  24. ^ "μPD77C20A, 7720A, 77P20 Digital Signal Processor". p. 4(3a-4). Retrieved 2023-12-25.

Read other articles:

This article needs additional citations for verification. Please help improve this article by adding citations to reliable sources. Unsourced material may be challenged and removed.Find sources: Polish mine detector – news · newspapers · books · scholar · JSTOR (February 2013) (Learn how and when to remove this template message) Men of the Royal Engineers in North Africa demonstrate the use of a mine detector (August 1942) Mine Detectors being assemble...

MangkubumiꦩꦁꦏꦸꦨꦸꦩꦶGusti Kanjeng RatuPutri Mahkota YogyakartaPemahkotaan5 Mei 2015Informasi pribadiKelahiranGusti Raden Ajeng Nurmalitasari24 Februari 1972 (umur 51)Kota Bogor, Jawa Barat, IndonesiaWangsaMataramNama lengkapGusti Kanjeng Ratu Mangkubumi Hamemayu Hayuning Bawana Langgeng ing MataramAyahHamengkubuwana XIbuRatu HemasPasanganWironegoro ​(m. 2002)​Anak Putri Raden Ajeng Artie Ayya Fatimasari Pangeran Raden Mas Drasthya Wironegoro Aga...

  لمعانٍ أخرى، طالع ظبي (توضيح). الظبي (بالإنجليزية: Antelope)‏ حيوان يشبه الغزال، وهو من الحيوانات المجترة آكلة العشب، ذی قرون مجوفة يغيرها سنويا. وارتبط الظبي في الأساطير المصرية القديمة بالآلهة: أنوبيس، وست، وأوزيريس، وحوريس. كما ارتبط في الأساطير الرومانية بالإلهة منير

The native form of this personal name is Orosz István. This article uses Western name order when mentioning individuals. István OroszOrosz in 2019Born (1951-10-24) 24 October 1951 (age 72)Kecskemét, HungaryEducationBudapest College of Applied ArtsKnown forGraphic design, animation, paintingOther namesUtisz István Orosz (born 24 October 1951) is a Hungarian painter, printmaker, graphic designer and animated film director. He is known for his mathematically inspired works...

Говард Нортвейт Говард Нортвейт Особисті дані Народження 21 червня 1990(1990-06-21)[1][2] (33 роки)   Ватс[d], Vindafjordd, Ругаланн, Норвегія[3] Зріст 186 см Вага 72 кг Громадянство  Норвегія Позиція крайній захисник[d] Інформація про клуб Поточний клуб «Гоффенгайм 1...

  لمعانٍ أخرى، طالع جاك كيلي (توضيح). جاك كيلي معلومات شخصية الميلاد 16 سبتمبر 1927(1927-09-16)مدينة نيويورك الوفاة 7 نوفمبر 1992 (65 سنة)هونتينغتون بيتش، أورانج، كاليفورنيا سبب الوفاة سكتة دماغية الجنسية الولايات المتحدة الأمريكية الزوجة ماي وين (1956–1964)  الحياة العملية المهنة...

Onder een zweving wordt verstaan de resultante van het superponeren (samenvoegen) van twee trillingen met slechts een klein verschil in frequentie. Het is daarmee een bijzonder geval van interferentie. Zwevingen treden onder meer op in de signaalverwerking wanneer van twee signalen de frequenties dicht bij elkaar liggen. Zweving kan optreden bij alle golven waarvoor het superpositieprincipe geldt, zoals bij geluidsgolven en elektromagnetische golven. Wiskundige beschrijving Afleiding van form...

FabienLahirFabien Yves Jerome Corbineau30 Oktober 1987 (umur 36)ParisKebangsaanPrancisNama lainFabien Yoon, Choi YoonAlmamaterUniversitas Paris 12 Val de MarnePekerjaanModel, Aktor Fabien Corbineau (lahir 30 Oktober 1987), lebih dikenal sebagai Fabien, adalah aktor, model, dan praktisi taekwondo asal Prancis yang saat ini berbasis di Korea Selatan. Ia menjadi anggota I Live Alone sejak tahun 2014.[1] Filmografi Acara TV/Drama Tahun Judul Peran Catatan 2008 East of Eden 2010 ...

Lindsay BurdgeWawancara Lindsay Burdge SXSW Film 2013LahirLindsay Michelle Burdge27 Juli 1984 (umur 39)[1]Pasadena, California, U.S.Pekerjaan Actress producer Tahun aktif2008–present Lindsay Michelle Burdge (lahir 27 Juli 1984) adalah aktris dan produser film berkebangsaan Amerika Serikat. Namanya dikenal melalui beberapa perannya dalam film independen antara lain A Teacher (2013), Wild Canaries (2014), The Midnight Swim (2014), The Invitation (2015), dan 6 Years (2015). L...

1st century AD king of Anuradhapura in Sri Lanka Yassalalaka TissaKing of AnuradhapuraReign52 – 60PredecessorChandamukhaSuccessorSubharajaDynastyHouse of VijayaFatherIlanagaReligionTheravāda Buddhism Yassalalaka Tissa was King of Anuradhapura in the 1st century, whose reign lasted from 52 to 60. He succeeded his brother Chandmukha and was succeeded by Subharaja. Yassalalaka, who came to the throne after assassinating his brother, is described in history as a playful as well as a vicious ru...

جزء من سلسلةصحة المرأةرمز صحّة المرأة الصحة الإنجابيّة والجنسيّة الصحة الإنجابيّة السبيل التناسليّ الأعضاء التناسليّة الخارجيّة البظر غطاء البظر الشفران الصغيران الشفران الكبيران المهبل عنق الرحم الرحم قناة فالوب مبيض أمراض الجهاز التناسلي صحة الأم حمل الحمل غير المق�...

2019 Pakistani Urdu language romantic-musical-drama film SuperstarTheatrical release posterDirected byMohammed EhteshamuddinScreenplay byAzaan Sami KhanProduced byMomina DuraidStarring Mahira Khan Bilal Ashraf Music byAzaan Sami Khan & Saad SultanProductioncompanyMomina & Duraid FilmsDistributed byHum FilmsRelease dates 6 August 2019 (2019-08-06) (Premiere) 12 August 2019 (2019-08-12) (Eid al-Adha) Running time135 minutes[1]CountryPakistan...

American aviator (1904–1991) This article needs additional citations for verification. Please help improve this article by adding citations to reliable sources. Unsourced material may be challenged and removed.Find sources: Domina Jalbert – news · newspapers · books · scholar · JSTOR (October 2011) (Learn how and when to remove this template message) Domina Cleophas Jalbert (1904–1991)[1] invented the ram-air inflated flexible wing, often c...

Island Shangri-La Hong Kong港島香格里拉大酒店港島香格里拉大酒店所在大樓。概要類型酒店建築風格高层地點香港島金鐘法院道太古廣場坐标22°16′38″N 114°09′51″E / 22.277222222222°N 114.16416666667°E / 22.277222222222; 114.16416666667起造日1986[1]竣工日1991年开放日1991年3月1日 [2]所有者嘉里集團(80%),太古地產(20%)[3]管理者香格里拉国际酒店管理公司...

Swiss footballer (born 1985) Lara Dickenmann Dickenmann with VfL Wolfsburg in 2018Personal informationFull name Lara Joy Dickenmann[1]Date of birth (1985-11-27) 27 November 1985 (age 38)Place of birth Kriens, SwitzerlandHeight 5 ft 5 in (1.65 m)Position(s) Midfielder, Full-backYouth career1993–2000 SC Kriens2000–2004 DFC SurseeCollege careerYears Team Apps (Gls)2004–2007 Ohio State Buckeyes Senior career*Years Team Apps (Gls)2006 New Jersey Wildcats 8 (8)2007...

Gaya atau nada penulisan artikel ini tidak mengikuti gaya dan nada penulisan ensiklopedis yang diberlakukan di Wikipedia. Bantulah memperbaikinya berdasarkan panduan penulisan artikel. (Pelajari cara dan kapan saatnya untuk menghapus pesan templat ini) Netralitas artikel ini dipertanyakan. Diskusi terkait dapat dibaca pada the halaman pembicaraan. Jangan hapus pesan ini sampai kondisi untuk melakukannya terpenuhi. (Pelajari cara dan kapan saatnya untuk menghapus pesan templat ini) Ini adalah ...

Esta página cita fontes, mas que não cobrem todo o conteúdo. Ajude a inserir referências. Conteúdo não verificável pode ser removido.—Encontre fontes: ABW  • CAPES  • Google (N • L • A) (Maio de 2022) Paróquias Patriarcais na Finlândia(Igreja Ortodoxa Russa na Finlândia) Igreja de São Nicolau em Helsinque Fundador Missionário de Novgorod na Carélia (Séc. XII); São Ticônio de Moscou (1921) Independência 1926 (sepa...

A História de Belém é sobre um município brasileiro da Região Norte do país, capital do estado do Pará, que teve suas origens no século XVII na região indígena de Mairi,[1] localizada à 160 km da linha do equador.[2] Até o início do período seiscentista, Portugal mostrava desinteresse nas terras localizadas na foz do Rio Amazonas ((que compreendem a atual Região Norte do Brasil); fato possivelmente ligado as políticas adotadas pelas cortes reais da primeira fase expansionista,...

Un Dassault Falcon 900 perteneciente al Grupo 45. El 45 Grupo de Fuerzas Aéreas de España es una unidad operativa del Ejército del Aire de España, una de las ramas de las Fuerzas Armadas de España, cuya misión es el transporte aéreo del rey de España, el presidente del Gobierno, vicepresidentes y ministros (también, en algunas ocasiones, de los miembros de la familia real o de la familia del presidente del Gobierno), y el mantenimiento de las aeronaves que utilizan. Tiene su base en ...

Stories of buried treasure on Oak Island, Nova Scotia, Canada Excavation work on Oak Island during the 19th century The Oak Island mystery is a series of stories of buried treasure and unexplained objects found on or near Oak Island in Nova Scotia. Since the 18th century, attempts have been made to find treasure and artifacts. Theories about artifacts present on the island range from pirate treasure to Shakespearean manuscripts to the Holy Grail or the Ark of the Covenant, with the Grail and ...