Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Return statement
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Overview == In [[C (programming language)|C]] and [[C++]], <code>return ''exp'';</code> (where <code>''exp''</code> is an [[Expression (programming)|expression]]) is a [[Statement (programming)|statement]] that tells a function to return execution of the program to the calling function, and report the value of <code>''exp''</code>. If a function has the return type [[Void 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.<ref name=msc/><ref name=mscpp/> Similar syntax is used in other languages including [[Modula-2]]<ref>{{cite book|title=Modula-2 for Pascal Programmers|first=R.|last=Gleaves|publisher=Springer|year=2012|isbn=9781461385318|page=71|url=https://books.google.com/books?id=pvzSBwAAQBAJ&pg=PA71}}</ref> and [[Python (programming language)|Python]].<ref name=python>{{cite book|title=Python in a Nutshell: A Desktop Quick Reference|first=Alex|last=Martelli|edition=2nd|publisher=O'Reilly Media|year=2006|isbn=9781449379100|page=73|url=https://books.google.com/books?id=JnR9hQA3SncC&pg=PA73}}</ref> In [[Pascal (programming language)|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.<ref name=scott>{{cite book|title=Programming Language Pragmatics|first=Michael L.|last=Scott|publisher=Morgan Kaufmann|year=2006|isbn=9780126339512|page=432|url=https://books.google.com/books?id=TLbvODF1uIEC&pg=PA432}}</ref> However, some versions of Pascal provide a special function <code>Exit(''exp'');</code> that can be used to return a value immediately from a function, or, without parameters, to return immediately from a procedure.<ref>{{cite book|title=Scientific Pascal|first=Harley|last=Flanders|publisher=Springer|year=2012|isbn=9781461224280|page=35|url=https://books.google.com/books?id=D7gPBwAAQBAJ&pg=PA35}}</ref> 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.<ref name=scott/><ref>{{Cite book | last = ANSI x3.9-1966 | title = USA Standard FORTRAN | publisher = American National Standards Institute | url = http://www.fh-jena.de/~kleine/history/languages/ansi-x3dot9-1966-Fortran66.pdf | access-date = May 5, 2010 | page = 14 | archive-date = May 15, 2011 | archive-url = https://web.archive.org/web/20110515143149/http://www.fh-jena.de/~kleine/history/languages/ansi-x3dot9-1966-Fortran66.pdf | url-status = dead}}</ref><ref>{{Cite book | last = ANSI x3.9-1978 | title = American National Standard β Programming Language FORTRAN | publisher = American National Standards Institute | url = http://www.fortran.com/fortran/F77_std/rjcnf.html | at = 15.8 RETURN Statement | access-date = December 11, 2007 | archive-url = https://web.archive.org/web/20131029134137/http://www.fortran.com/fortran/F77_std/rjcnf.html | archive-date = October 29, 2013 | url-status = dead }}</ref> In some other languages a user defined [[output parameter]] is used instead of the function identifier.<ref>{{cite journal | last = Sakkinen | first = Markku | date = March 1989 | doi = 10.1145/66083.66087 | issue = 3 | journal = ACM SIGPLAN Notices | pages = 55β56 | publisher = Association for Computing Machinery | title = How to best return the value of a function | volume = 24| doi-access = free }}</ref> [[Oberon (programming language)|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.<ref>{{cite report|title=The Programming Language Oberon|first=Niklaus|last=Wirth|author-link=Niklaus Wirth|date=May 3, 2016|url=https://people.inf.ethz.ch/wirth/Oberon/Oberon07.Report.pdf|contribution=10. Procedure declarations|page=11}}</ref> Some [[expression-oriented programming language]], such as [[Lisp (programming language)|Lisp]], [[Perl]] and [[Ruby (programming language)|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 (programming language)|Python]], the value <code>None</code> is returned when the return statement is omitted,<ref name=python/> while in JavaScript the value <code>undefined</code> is returned. In [[Windows PowerShell]] all evaluated expressions which are not captured (e.g., assigned to a variable, [[Type conversion|cast]] to [[Void type|void]] or [[Pipeline (Unix)|piped]] to [[/dev/null|$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 (computing)|scalar]] context where the calling code expects one value, a [[List (computing)|list]] context where the calling code expects a list of values and a [[Void type|void]] context where the calling code doesn't expect any return value at all. A subroutine can check the context using the <code>wantarray</code> 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 data type|Boolean]], number, [[String (computer science)|string]], and various [[Reference (computer science)|reference]] types contexts. Also, a context-sensitive [[Object (computer science)|object]] can be returned using a contextual return sequence, with [[lazy evaluation]] of scalar values. Many [[operating system]]s let a program return a result (separate from normal [[Standard streams|output]]) when its process terminates; these values are referred to [[exit status]]es. 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).
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)