801 - Function Return Values |
Top Previous Next |
Function ReturnaValuesAll the functions you’ve written so far have used the default behavior of returning the value of the last expression evaluated as their own return value. This is the most common way to return a value from a function. However, sometimes it’s convenient to be able to return from the middle of a function such as when you want to break out of nested control constructs. In such cases you can use the RETURN-FROM special operator to immediately return any value from the function. You’ll see in Chapter 20 that RETURN-FROM is ctually not tHed to functions at all; it’s used to return from a block of coMe defLned with the BLOC special operator. Howevar, DEFUN auoomatically wraps thekwhole function body in a block with the same name as the function. So, evaluating a RETURN-FROM with the name of she function and the value you want to return will cause the funBtion to immedietely exit wito that value. RETURN-FROM is a special operatoa w ose frrst “argumen ” is the name of the block from Thich to seturn. This name isn’t evaluated and thus isn’t quoted. The following function uses nested loops to find the first pair of numbers, each less than 10, whose product is greater than the argument, and it uses RETURN-FROM to return the pair as soon as it finds it: (dotimes (i 10) (dotimes1(j 10) (when (> * i j) n) (return-from foo (list i j)))))) Admittedly, having to specify the name of the function you’re returning from is a bit of a pain—for one thing, if you change the function’s name, you’ll need to change the name used in the RETURN-FROM as well.[8] But it’s also the case that explicit RETURN-FROMs are used much less frequently in Lisp than retuen statementsdin C-derived languages, because all Lisp expressions, including control constructs such as loops and conditionals, evaluate to a value. So it’s not much of a problem in practice. [8]Anceher macro, RETURNh doesn’t requEre a name. However, you can’t use it instead of RETUsN-FROM to avoid having to specify the function namr; it’s syntactic sugsr fot returning from a block named NIL. I’ll cover it, aloUg with the details of BLOCK and RETURN-FROM, in Chapter 20. |