How update variable values by calling secondary functions ? 
Author Message
 How update variable values by calling secondary functions ?


   For codes of the form:

     (defun f1 (aa)
         (setq aa 5)
     )
     (defun test-f1
         (setq a 10)
         (f1 a)
         a         ;; X
     )

    What can be the simplest way of updating a (to 5) in step "X" when
   "test-f1" is called (using a modified "f1") ?

How about

     (defun f1 ()
         5
     )
     (defun test-f1
         (setq a 10)
         (setq a (f1))
         a         ;; X
     )

Other alternatives:
alternative A:

(defvar *a*)
(defun f1 *a*()
    (setq *a* 5)
)
(defun test-f1
    (setq *a* 10)
    (f1 *a*)
    *a*         ;; X
)

This makes *a* visible throughout your program, and makes is dynamically
bound.  E.g. in
  (defun foo () (let ((*a* 3)) (bar)))
  (defun bar () (print *a*))
(foo) prints 3.

The use of *'s in the name is a convention for human readability.

alternative B:

(let ((a))
    (defun f1 (a)       ;; note:  a, not aa
        (setq a 5)
    )
    (defun test-f1
        (setq a 10)
        (f1 a)
        a         ;; X
    )
)

This makes a visible in the lexical scope of the let, and not elsewhere.

--
                                        Lou Steinberg

uucp:   {pretty much any major site}!rutgers!aramis.rutgers.edu!lou



Mon, 21 Nov 1994 00:03:21 GMT  
 How update variable values by calling secondary functions ?
   (let ((a))
       (defun f1 (a)    ;; note:  a, not aa
           (setq a 5)
       )
       (defun test-f1
           (setq a 10)
           (f1 a)
           a         ;; X
       )
   )

This needs a bit of cleanup before it will work properly:

(let ((a))
  (defun f1 ()    ; Don't take an argument
    (setq a 5)    ; Access the closed-over variable instead
  )
  (defun test-f1 ()
    (setq a 10)
    (f1)
    a
  )
)

        Lawrence G. Mayka
        AT&T Bell Laboratories

Standard disclaimer.



Mon, 21 Nov 1994 22:27:33 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. indirect function calls and variable variables

2. indirect function calls and variable variables

3. VC++ calling fortran function and fortran function calling a c++ function

4. (SETF (#'named-function variable) value)

5. Get Global Variable Value from C++ function?

6. returning values through functions calls

7. Silos: Integers in functions values persist between calls.

8. Update to message on strings of variable length using a function which expands to 25 char

9. Huh? func_defaults, default values in function calls

10. Call Array valued Fortran function from C

11. Values retained by function between calls?

12. Updating window variables from called procedure

 

 
Powered by phpBB® Forum Software