Operator (computer programming): Difference between revisions

Content deleted Content added
m Reverting possible vandalism by 217.165.112.19 to version by Nbarth. Report False Positive? Thanks, ClueBot NG. (2367970) (Bot)
No edit summary
Line 1:
{{refimprove|date=January 2012}}
 
Hi Barbie
[[Programming languages]] typically support a set of '''operators''': constructs which behave generally like functions, but which differ syntactically or semantically from usual functions. Common simple examples include arithmetic (addition with +, comparison with &gt;) and logical operations (such as AND or &amp;&amp;). More involved examples include assignment (usually = or :=), [[Field (computer science)|field]] access in a record or object (usually .), and the [[scope resolution operator]] (often <code>::</code>). Languages usually define a set of built-in operators, and in some cases allow user-defined operators.
Hi Ken!
Do you wanna go for a ride?
Sure Ken!
Jump in...
 
I'm a Barbie girl, in the Barbie world
==Syntax==
Life in plastic, it's fantastic!
[[Syntax (programming languages)|Syntactically]] operators usually contrast to [[Function (computer science)|functions]]. In most languages, functions may be seen as a special form of prefix operator with fixed [[Order of operations|precedence]] level and associativity, often with compulsory [[Bracket#Parentheses .28 .29|parentheses]] e.g. <code>Func(a)</code> (or <code>(Func a)</code> in [[Lisp (programming language)|LISP]]). Most languages support programmer-defined functions, but cannot really claim to support programmer-defined operators, unless they have more than prefix notation and more than a single precedence level. Semantically operators can be seen as special form of function with different calling notation and a limited number of parameters (usually 1 or 2).
You can brush my hair, undress me everywhere
Imagination, life is your creation
Come on Barbie, let's go party!
 
I'm a Barbie girl, in the Barbie world
The position of the operator with respect to its operands may be [[Polish notation|prefix]], [[infix notation|infix]] or [[postfix notation|postfix]], and the syntax of an [[expression (computer science)|expression]] involving an operator depends on its [[arity]] (number of [[operand]]s), precedence, and (if applicable), [[Operator associativity|associativity]]. Most programming languages support [[binary operation|binary operators]] and a few [[unary operation|unary operators]], with a few supporting more operands, such as the [[?:]] operator in C, which is ternary. Commonly, unary operations are prefix, such as unary minus <code>-x</code>, or occasionally postfix, such as [[post-increment]] <code>x++</code>; and binary operations are infix, such as <code>x + y</code> or <code>x = y</code>. Infix operations of higher arity require additional symbols, such as the [[ternary operator]] ?: in C, written as <code>a ? b : c</code> – indeed, this is the only common example, it is often referred to as ''the'' ternary operator. Prefix and postfix operations can easily support any desired arity, however, such as <code>1 2 3 4 +</code>.
Life in plastic, it's fantastic!
You can brush my hair, undress me everywhere
Imagination, life is your creation
 
I'm a blond bimbo girl, in a fantasy world
Occasionally<ref>[http://reference.wolfram.com/mathematica/tutorial/OperatorInputForms.html#11350 Mathematica reference]</ref><ref>[http://maxima.sourceforge.net/docs/manual/en/maxima_7.html#SEC36 Maxima reference]</ref> parts of a language may be described as "matchfix" or "circumfix"<ref>[http://mythryl.org/my-Prefix__Postfix_and_Circumfix_Operators.html]</ref><ref>[http://doc.perl6.org/op/circumfix/(%20).html]</ref> operators, either to simplify the language's description or implementation. A circumfix operator consists of two or more parts which enclose its operands. Circumfix operators have the highest precedence, with their contents being evaluated and the resulting value used in the surrounding expression. The most familiar circumfix operator are the parentheses mentioned above, used to indicate which parts of an expression are to be evaluated before others. Another example from physics is the [[Inner product space|inner product]] notation of Dirac's [[bra–ket notation]]. Circumfix operators are especially useful to denote operations that involve many or varying numbers of operands.
Dress me up, make it tight, I'm your dolly
You're my doll, rock'n'roll, feel the glamour in pink,
Kiss me here, touch me there, hanky panky...
You can touch, you can play, if you say: "I'm always yours"
 
(uu-oooh-u) [2x]
The specification of a language will specify the syntax the operators it supports, while languages such as [[Prolog]] that support programmer-defined operators require that the syntax be defined by the programmer.
 
I'm a Barbie girl, in the Barbie world
==Semantics==
Life in plastic, it's fantastic!
The semantics of operators particularly depends on value, evaluation strategy, and argument passing mode (such as boolean short-circuiting). Simply, an [[Expression (computer science)|expression]] involving an operator is evaluated in some way, and the resulting [[Value (computer science)|value]] may be just a value (an r-value), or may be an object allowing assignment (an l-value).
You can brush my hair, undress me everywhere
Imagination, life is your creation
 
Come on Barbie, let's go party!
In simple cases this is identical to usual function calls; for example, addition <code>x + y</code> is generally equivalent to a function call <code>add(x, y)</code> and less-than comparison <code>x &lt; y</code> to <code>lt(x, y)</code>, meaning that the arguments are evaluated in their usual way, then some function is evaluated and the result is returned as a value. However, the semantics can be significantly different. For example, in assignment <code>a = b</code> the target <code>a</code> is not evaluated, but instead its ''___location'' (address) is used to store the value of <code>b</code> – corresponding to [[call-by-reference]] semantics. Further, an assignment may be a statement (no value), or may be an expression (value), with the value itself either an r-value (just a value) or an l-value (able to be assigned to). As another example, the [[scope resolution operator]] :: and the element access operator . (as in <code>Foo::Bar</code> or <code>a.b</code>) operate not on values, but on ''names'', essentially [[call-by-name]] semantics, and their value is a name.
(Ah-ah-ah-yeah)
Come on Barbie, let's go party!
(uu-oooh-u) [2x]
Come on Barbie, let's go party!
(Ah-ah-ah-yeah)
Come on Barbie, let's go party!
(uu-oooh-u) [2x]
 
Make me walk, make me talk, do whatever you please
Use of l-values as values of operator expressions is particularly notable in array indexing and unary [[increment and decrement operators]]. In C, for instance, the following statements are legal and well-defined, and depend on the fact that these operators return an l-value:
I can act like a star, I can beg on my knees
Come jump in, bimbo friend, let us do it again,
Hit the town, fool around, let's go party
You can touch, you can play, if you say: "I'm always yours"
You can touch, you can play, if you say: "I'm always yours"
 
Come on Barbie, let's go party!
(Ah-ah-ah-yeah)
Come on Barbie, let's go party!
(uu-oooh-u) [2x]
Come on Barbie, let's go party!
(Ah-ah-ah-yeah)
Come on Barbie, let's go party!
(uu-oooh-u) [2x]
 
I'm a Barbie girl, in the Barbie world
Life in plastic, it's fantastic!
You can brush my hair, undress me everywhere
Imagination, life is your creation
 
I'm a Barbie girl, in the Barbie world
Life in plastic, it's fantastic!
You can brush my hair, undress me everywhere
Imagination, life is your creation
 
Come on Barbie, let's go party!
(Ah-ah-ah-yeah)
Come on Barbie, let's go party!
(uu-oooh-u) [2x]
Come on Barbie, let's go party!
(Ah-ah-ah-yeah)
Come on Barbie, let's go party!
(uu-oooh-u) [2x]
 
Oh, I'm having so much fun!
Well Barbie, we're just getting started
Oh, I love you Ken!ors return an l-value:
<source lang=c>
y = ++x++;