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
Parameter (computer programming)
(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!
===Alternatives=== There are various alternatives to the use cases of output parameters. For returning multiple values from a function, an alternative is to return a [[tuple]]. Syntactically this is clearer if automatic sequence unpacking and [[parallel assignment]] can be used, as in [[Go (programming language)|Go]] or Python, such as: <syntaxhighlight lang="python"> def f(): return 1, 2 a, b = f() </syntaxhighlight> For returning a value of one of several types, a [[tagged union]] can be used instead; the most common cases are [[nullable type]]s ([[option type]]s), where the return value can be null to indicate failure. For exception handling, one can return a nullable type, or raise an exception. For example, in Python one might have either: <syntaxhighlight lang="python"> result = parse(s) if result is None: # exception handling </syntaxhighlight> or, more idiomatically: <syntaxhighlight lang="python"> try: result = parse(s) except ParseError: # exception handling </syntaxhighlight> The micro-optimization of not requiring a local variable and copying the return when using output variables can also be applied to conventional functions and return values by sufficiently sophisticated compilers. The usual alternative to output parameters in C and related languages is to return a single data structure containing all return values.<ref name=hallam/> For example, given a structure encapsulating width and height, one can write: <syntaxhighlight lang="c"> WidthHeight width_and_height = F(x); </syntaxhighlight> In object-oriented languages, instead of using input/output parameters, one can often use [[call by sharing]], passing a reference to an object and then mutating the object, though not changing which object the variable refers to.<ref name=CA1021 />
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)