Search code examples
functionproceduresicp

(SICP) What is the difference between functions and procedures?


I am currently working my way through Structure and Interpretation of Computer Programs doing both the book and lectures from Brian Harvey(who is hilarious at points), however I have yet to truly have my "aha! moment" with differentiating functions and procedures.

Now I have done my research outside of the lectures and readings and came across a few different posts regarding this same question, but all seem to branch out into separate discussions/opinions on the true interpretation or outdated definitions. A general answer I have seen is that functions return a value and procedures do not, however that does not clear up much for me and most users on that response seemed to have some arguments against that answer.

Diving into higher order procedures within the text and lectures I completely understand the concept and the power this makes available, however I am confused because I will hear "Higher Order Procedures" and "Higher Order Functions". Brian Harvey also mentioned that "A higher order procedure represents a higher order function".

I understand that the two functions below are the same function, but different procedures.

f(x) = 2x + 6
g(x) = 2(x + 3)

Now below, make-adder is referred to as a procedure with a num as it's formal parameter. The domain of make-adder is numbers, the range is procedures. I guess what is really stumping me is he refers to the lambda expression as exactly that, a lambda expression, but make-adder is returning a procedure?

(define (make-adder num)
  (lambda (x) (+ x num))

(define plus3 (make-adder 3))
(plus3 8)

I thought I had a clear understanding until a few references to procedures during the higher order procedures lessons and that has since fogged things up.

Any help differentiating the two with a possible example? Thank you!


Solution

  • TL; DR: A procedure and function means the same in the context of SICP.

    In mathematics a function is something that you apply with arguments and returns a value and would always return the same values to the same arguments. You could replace it with a map between the arguments to the result.

    In programming languages like Scheme or JavaScript the use of the word function is not correct for all code that has some kind of side effects or where the return does isn't consistent with the arguments.

    A procedure is a more generic term so you cannot say that a procedure needs to have referential transparency so that it can be seen as a mathematical function and thus both Scheme and JavaScript has procedures and not functions. Eg. subroutine in x86 intel platform is a procedure. It allows no arguments and not return value, just jump and return. However C uses code to manipulate the stack to be able to pass arguments and get a return value and in that sense you can emulate "a function", but they did not remove the possibility of the return not being the same for every input and thus you can implement a "c function" that is not a function, but you can call it a procedure.