2

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.
jon hanson
  • 8,722
  • 2
  • 37
  • 61
David542
  • 104,438
  • 178
  • 489
  • 842
  • 1
    A function in Python can be defined by either a statement (def) or expression (lambda), they're not really two comparable things. – jonrsharpe Oct 21 '21 at 20:54
  • 1
    While being different things both have properties in common: They evaluate to a value, are composable and first class. Statements have none of these. –  Oct 22 '21 at 12:18

3 Answers3

6

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.

Silvio Mayolo
  • 62,821
  • 6
  • 74
  • 116
  • thanks for this. What do you mean by: `Functions exist at runtime; expressions are purely a parsing construct.` ? For example, what if the expression is `now() + interval 1 day` ? – David542 Oct 21 '21 at 20:58
  • I don't understand the question, I'm afraid. That's not valid Python syntax. Though if it was `now() + 1`, then I'd say it's an addition expression, where the right-hand is a call expression and the left-hand is a numerical literal expression. – Silvio Mayolo Oct 21 '21 at 21:01
  • sure, I was writing it in SQL, but in python it would be: `datetime.datetime.now() + datetime.timedelta(days=1)` – David542 Oct 21 '21 at 21:03
  • 2
    In many languages, you certainly can assign expressions to variables, eg in R: `x <- quote(1+1)`. This kind of "programming on the language" or "non-standard evaluation" can be useful in many contexts – Hong Ooi Oct 22 '21 at 00:19
  • @HongOoi That is very true. Once you get into reflection and macro expansion, then things get *really* fun. – Silvio Mayolo Oct 22 '21 at 00:20
2

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.

Vaibhav Gupta
  • 1,035
  • 1
  • 8
  • 16
1

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)

jon hanson
  • 8,722
  • 2
  • 37
  • 61