Jump to content

Expression (computer science)

From Wikipedia, the free encyclopedia

In computer science, an expression is a syntactic notation in a programming language that may be evaluated to determine its value of a specific semantic type. [1][2] It is a combination of one or more numbers, constants, variables, functions, and operators that the programming language interprets (according to its particular rules of precedence and of association) and computes to produce ("to return", in a stateful environment) another value. In simple settings, the resulting value is usually one of various primitive types, such as string, boolean, or numerical (such as integer, floating-point, or complex).

Expressions are often contrasted with statementssyntactic entities that have no value (an instruction).

Definition

[edit]

Like in mathematics, an expression is used to denote a value to be evaluated for a specific value type accepted syntactically by a object language. In some cases an expression can't be fully evaluated; in this cases, the value is undefined, even though the calculation was effected and finished.[1]: 26 

The process of evaluating an expression is called evaluation; it can occur in different contexts, such as definition and initialization [needs independent confirmation]. Unlike initialization, evaluation is not restricted to the first occurrence.

Examples

[edit]

2 + 3 is both an arithmetic and programming expression, which evaluates to 5. A variable is an expression because it denotes a value in memory, so y + 6 is also an expression. An example of a relational expression is 4 ≠ 4, which evaluates to false.[3][4]

C

[edit]

You can see how can the parts of the code be a expression.

int main(void) {

    int y = 10; // The expression "y = 10";
    int x = 40; // The expression "x = 40";
    
    int result = x + y; // The expression "result = x + y" is evaluated;

    return 0; // When a function is called, its return expression is evaluated and the call expression yields that value to the caller.
}

In C (and many others programming languages), the "=" is consider operator like in mathematics; more specifically, a binary operator.

Void as a result type

[edit]

In C and most C-derived languages, a call to a function with a void return type is a valid expression, of type void.[5] Values of type void cannot be used, so the value of such an expression is always thrown away.

Side effects and elimination

[edit]

In many programming languages, a function, and hence an expression containing a function, may have side effects. An expression with side effects does not normally have the property of referential transparency. In many languages (e.g. C++), expressions may be ended with a semicolon (;) to turn the expression into an expression statement. This asks the implementation to evaluate the expression for its side-effects only and to disregard the result of the expression (e.g. x+1;) unless it is a part of an expression statement that induces side-effects (e.g. y=x+1; or func1(func2());).

Caveats

[edit]

The formal notion of a side effect is a change to the abstract state of the running program.

Another class of side effects are changes to the concrete state of the computational system, such as loading data into cache memories. Languages that are often described as "side effect–free" will generally still have concrete side effects that can be exploited, for example, in side-channel attacks.

Furthermore, the elapsed time evaluating an expression (even one with no other apparent side effects), is sometimes essential to the correct operation of a system, as behaviour in time is easily visible from outside the evaluation environment by other parts of the system with which it interacts, and might even be regarded as the primary effect such as when performing benchmark testing.

It depends on the particular programming language specification whether an expression with no abstract side effects can legally be eliminated from the execution path by the processing environment in which the expression is evaluated.

See also

[edit]

References

[edit]
  1. ^ a b Mitchell, John C. (2002). "3.4.1 Statements and Expressions". Concepts in Programming Languages (1st ed.). Cambridge: Cambridge University Press. p. 26. ISBN 0521780985. OCLC 49394640.
  2. ^ Gabbrielli, Maurizio; Martini, Simone (2010). Programming Languages — Principles and Paradigms. London: Springer. p. 120.
  3. ^ Javascript expressions, Mozilla Archived 2012-03-09 at the Wayback Machine Accessed July 6, 2009
  4. ^ Programming in C Archived 2015-01-09 at the Wayback Machine Accessed July 6, 2009
  5. ^ ISO/IEC 9899:1999 section 6.3.2.2, accessed August 31, 2009
[edit]
  • This article is based on material taken from Expression at the Free On-line Dictionary of Computing prior to 1 November 2008 and incorporated under the "relicensing" terms of the GFDL, version 1.3 or later.