As I said,
'The construction looks fine at first glance.'
the V6 variable is the only one that I had to look at twice :)
Something that will make your life easier :
Prefix ALL your nested functions with an underscore
eg: _subb
Some projects I will also prefix variable with recognisable characters.
This helps when you're dealing with 1000+ lines of code that share variables.
trivia:
Did you know that you could ( if you wanted to do so) just have all the variables listed in the main ? ..
But I prefer the way you've done it, declaring the variables for each nested function.
I prefer Example 1.
nested functions in lisp were slower than un-nested functions.
Regards,
(defun fun1 (a0 / v0)
(setq a0 ...)
...
(setq v0 ...)
...
v0
)
(defun fun2 (a0 / v0 v1 v2)
(setq v0 ...)
(setq v1 (* (atoi a0) 13))
(setq v2 (fun3 v2))
...
v2
)
(defun fun3 (a0 / v0 v1 v2)
(setq v0 ...)
(setq v1 ...)
(setq v2 (* (atoi a0) 10))
...
v2
)
(defun main ( / v0 v1)
(setq v0 ...)
(setq v1 (fun2 v0))
...
)
Hi Roy,Do you mean global variables used by non-nested functions as opposed to variables within the scope of a main function that are (also) used by its nested functions?
The issue I've noticed with non-nested functions in code I've reviewed is that global variables are not treated findly and the scope of variables usually becomes an issue.
I prefer example 3 (see below).
In many code samples on this forum you will find nested functions. But there are some arguments against this practice:
- As Kerry mentions: nested functions will make the code slower. Every time the main function is entered the nested functions will be redefined.
- Unnested functions can be reused by other functions allowing for a modular coding style.
- It is easier to debug unnested functions.
- Related to the previous: Nested functions can easily lead to spaghetti code.
EXAMPLE 3:Code: [Select](defun fun1 (a0 / v0)
(setq a0 ...)
...
(setq v0 ...)
...
v0
)
(defun fun2 (a0 / v0 v1 v2)
(setq v0 ...)
(setq v1 (* (atoi a0) 13))
(setq v2 (fun3 v2))
...
v2
)
(defun fun3 (a0 / v0 v1 v2)
(setq v0 ...)
(setq v1 ...)
(setq v2 (* (atoi a0) 10))
...
v2
)
(defun main ( / v0 v1)
(setq v0 ...)
(setq v1 (fun2 v0))
...
)
ROYIf you mean that a function with arguments is too hard to understand for a beginner, and I think that is what you mean, I disagree.
formal parameter & actual parameter ?
Not for beginners ,,,,,
I prefer example 3 (see below).
In many code samples on this forum you will find nested functions. But there are some arguments against this practice:
- As Kerry mentions: nested functions will make the code slower. Every time the main function is entered the nested functions will be redefined.
- Unnested functions can be reused by other functions allowing for a modular coding style.
- It is easier to debug unnested functions.
- Related to the previous: Nested functions can easily lead to spaghetti code.
EXAMPLE 3:Code: [Select](defun fun1 (a0 / v0)
(setq a0 ...)
...
(setq v0 ...)
...
v0
)
(defun fun2 (a0 / v0 v1 v2)
(setq v0 ...)
(setq v1 (* (atoi a0) 13))
(setq v2 (fun3 v2))
...
v2
)
(defun fun3 (a0 / v0 v1 v2)
(setq v0 ...)
(setq v1 ...)
(setq v2 (* (atoi a0) 10))
...
v2
)
(defun main ( / v0 v1)
(setq v0 ...)
(setq v1 (fun2 v0))
...
)
So many repeated variable name . Why ?Since all functions in Example 3 are independent functions there is no reason why the same argument and variable names shouldn't be reused. This practice is in fact not uncommon. Examples of some frequently used variable names in Lisp code: i, lst, obj and str.
So many repeated variable name . Why ?Since all functions in Example 3 are independent functions there is no reason why the same argument and variable names shouldn't be reused. This practice is in fact not uncommon. Examples of some frequently used variable names in Lisp code: i, lst, obj and str.
; (Add_2_Numbers 3 6) => 9
(defun Add_2_Numbers (num1 num2)
(+ num1 num2)
)
; (Pi_Divided_By_2) => 1.5708
(defun Pi_Divided_By_2 ()
(/ pi 2.0)
)
; (Get_DateDay) => 2457222
(defun Get_DateDay ()
(fix (getvar 'date))
)
@ AIberto:
I think you should think about creating a function that returns a single value. I would advise that the output be a number in the same format as the DATE variable. It would make the most sense to revise Lee's LM:InternetTime for this purpose...
Tip: Take a good look at your code. You are unnecessarily calling functions twice.
You can either return the two values in a list:Code - Auto/Visual Lisp: [Select]
) )
...or pass the function two quoted symbols to which the values will be assigned:Code - Auto/Visual Lisp: [Select]
) ) nil )Code - Auto/Visual Lisp: [Select]
(get_time 'mydate 'myjuliandate)
For the latter, note that the supplied symbols cannot be the same as the symbols used to represent the function parameters, else the values will remain local to the function.
QuoteFor the latter, note that the supplied symbols cannot be the same as the symbols used to represent the function parameters, else the values will remain local to the function.I can't understand. Can you specifically talk about it? or give me an example ? Thanks.
QuoteFor the latter, note that the supplied symbols cannot be the same as the symbols used to represent the function parameters, else the values will remain local to the function.I can't understand. Can you specifically talk about it? or give me an example ? Thanks.
Observe:Code - Auto/Visual Lisp: [Select]
...or pass the function two quoted symbols to which the values will be assigned:Code - Auto/Visual Lisp: [Select]
) ) nil )
...or pass the function two quoted symbols to which the values will be assigned:Code - Auto/Visual Lisp: [Select]
) ) nil )
Lee, Why use "set" rather than "setq" ?
Lee, Why use "set" rather than "setq" ?
Because I require both arguments supplied to the set function to be evaluated: the first argument yields the symbol to which the value should be assigned (that is, the quoted symbol supplied as an argument to the 'get_time' function); the second argument yields the value (as per setq).
If setq were used, the first argument would not be evaluated and the corresponding value would be assigned to the 'outsym1' or 'outsym2' symbols.
Observe:Code - Auto/Visual Lisp: [Select]
(defun thedate (date1 sep1 /)
(setq date1 (rtos date1 2 8))
(strcat (substr date1 1 4)
sep1
(substr date1 5 2)
sep1
(substr date1 7 2)
)
)
(thedate (getvar "cdate") "") and (dtoj (atoi (thedate (getvar "cdate") "")))
(itoa (fix (getvar 'cdate))) and (fix (getvar 'date))
...or pass the function two quoted symbols to which the values will be assigned:Code - Auto/Visual Lisp: [Select]
) ) nil )
Lee, Why use "set" rather than "setq" ?
Because I require both arguments supplied to the set function to be evaluated: the first argument yields the symbol to which the value should be assigned (that is, the quoted symbol supplied as an argument to the 'get_time' function); the second argument yields the value (as per setq).
If setq were used, the first argument would not be evaluated and the corresponding value would be assigned to the 'outsym1' or 'outsym2' symbols.
Observe:Code - Auto/Visual Lisp: [Select]