In computer programming, an expression is any combination of special characters, variables, and function calls that is a value or produces a value.
Here are a couple of examples of expressions:
15
, which is the value 15
.
"Hello World"
, which is the value "Hello World"
.
3 + 2
, which produces the value 5
.
3 > 2
, which produces the value true
, because 3
is larger than 2
.
health + 1
, which produces the sum of the variable health
and 1
.
The code var health = 0
is not an expression because it does not produce a value: it creates a variable named health
and assigns the value 0
to it.
You can combine expressions to form more complex expressions. For example, the expression 3 + 4
is a combination of the expressions 3
and 4
using the +
operator.
In this code listing, each line contains an expression. The code uses values directly or calls functions that return values, but of course, it also works if you use variables instead of values.
3
3 + 4 * 5
"Hello, " + "world!"
true and false
randf_range(10.0, 100.0)
It's important to note that not every piece of code produces a value. For instance, defining a function or a variable is not an expression because it does not produce a value.
When you see an error like "expected expression" while coding, it means that the computer expects you to write a piece of code that produces a value but that you wrote something that does not.See Also
Related terms in the Glossary