help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Optional Arguments


From: tomas
Subject: Re: Optional Arguments
Date: Mon, 7 Dec 2020 16:51:08 +0100
User-agent: Mutt/1.5.21 (2010-09-15)

On Mon, Dec 07, 2020 at 04:06:13PM +0100, pietru@caramail.com wrote:

[...]

> How can one get computed values from a function then?

Either you return it (in your case, e.g. by returning a pair)

  (cons ma mb)

or (less preferable) by modifying variables "outside" of the
function. You have to take carefully into account what kind
of binding the program runs under, or to use "global" (in
the sense of program scope: buffer-local variables count as
global here) variables, and you have a very good Petri dish
for all sort of nasty bugs ;-)

Under lexical binding, this, for example, would do:

  (let ((ma 0)
        (mb 0))
  
    (defun foo ()
      (setq ma 5)
      (setq mb 7))
  
    (foo)
    (message "ma: %d mb: %d" ma mb))

(the function `foo' "sees" the variables `ma', `mb' set up in
the enclosing scope). Only recommended for small, tightly knit
snippets: the (human) reader should be able to "see" that too.

Cheers
 - t

Attachment: signature.asc
Description: Digital signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]