Differences between Expression vs Function

Viewed 282

What is the formal difference between an expression and a function? I know the difference by looking at it, but I'm looking for a thorough understanding of it. For example, showing some examples from Scheme or Python:

; scheme
(display "hello")                          # expression
((lambda () (display "hello")))            # unnamed lambda
(define hi (lambda () (display "hello")))  # named lambda

# python
>>> print ('hello')          
>>> lambda: print ('hello')
>>> hi = lambda: print ('hello')

In my rudimentary thinking, I thought the differences are:

  1. A function has a name and can be referred to (though an expression could be assigned to a variable?)
  2. A function can take parameters (is an expression able to?)
  3. A function can have a scope/encapsulation and contain multiple statements.
3 Answers

You might be comparing apples to oranges here. An expression is a syntactic form. It's a part of your code and describes how the code is parsed. a + b and print(x) are expressions, but so are a, b, and x. Expressions are generally made of smaller expressions, often several layers deep.

A function, on the other hand, is concerned with semantics, not syntax. It's a runtime value. One might say that

lambda x: x + 1

is a function. To be completely correct, I would say that those letters parse as an expression which, when evaluated by the Python interpreter, produces a function. But that's quite wordy, so we usually skip the middle man and just say the lambda is a function.

It makes no sense to assign an expression to a variable. If I write x = 1 + 1, I'm not assigning the expression 1 + 1 to the variable. I'm assigning the result of calculating one plus one to that variable. On the other hand, if I write x = lambda: 2, then I am actually assigning a function to the variable x. Functions exist at runtime; expressions are purely a parsing construct.

First answering your questions:

  1. A function has a name and can be referred to (though an expression
    could be assigned to a variable?) --- Yes, expression can be assigned to a variable but unlike function you cannot call an expression with a parameter.
  2. A function can take parameters (is an expression able to?) --- No, expression is not able to take parameter.
  3. A function can have a scope/encapsulation and contain multiple statements. -- True

Functional programming (FP) is all about expressions since in FP everything reduces to an expression. Functions are used as objects in FP. That is they are often passed around within a program in much the same way as other variables. For example - Python provides several functions which enable a functional approach to programming say a function map(aFunction, aSequence)

This function applies a Python function, aFunction to each member of aSequence. The expression:

L = map(eggs, spam)
print L

Results in a new list (in this case identical to spam) being returned in L.

If you think about it in the context of a pure FP, one that doesn't allow side-effects (therefore statements aren't required), then a function is a just a value with a specific type (A -> B). An expression is some combination of symbols that reduces to a value.

Consider the following examples:

a = 1 + (2 * 3);
b = a * 10;

add1 = x -> x + 1;

applyTwice = f -> x -> f(f(x))

add2 = applyTwice(add1)

a, b, add1, applyTwice & add2 are all names have been defined to hold the value computed by specific expressions. The first two expressions reduced to 7 and 70 respectively, and the latter three are function values. The final one, add2, is defined via an expression that reduces to a function value.

With regards to the points you raised:

  • A function has a name and can be referred to (though an expression could be assigned to a variable?)

Most languages allow you assign any expression to a name.

  • A function can take parameters (is an expression able to?)

Functions have parameters, unlike non-functions. As noted above, an expression can reduce to a function value.

  • A function can have a scope/encapsulation and contain multiple statements.

The function body is essentially just an expression, albeit one that contains unbound variables that reference the function parameters. Statements are a distraction - some languages also allow you to assign the result of a series of statements to a non-function variable, e.g.:

result = {
    a = f();
    b = g(a);
    h(b);
}

(i.e. the return value of h(b) is assigned to result)

Related