Author Topic: Best way to create return values from a function  (Read 441 times)

0 Members and 1 Guest are viewing this topic.

keithsCADservices

  • Newt
  • Posts: 45
Best way to create return values from a function
« on: February 14, 2024, 12:08:28 AM »
Lately I've been using the "eval" function. If I have built a list within the function I will either use the list function itself (if the list is created at the end of the function) or use something like (eval 'variableContainingList) if the list was already created earlier. "Eval" feels like the cleanest solution but can cause some inconsistency when dealing with lists (and having to use the quote function). Recently I've started doing something like "(setq returnValue....".

There is no formal way to "return" a value from a function, as far as I know (compared to C# for example). Curious how you guys do it.

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Best way to create return values from a function
« Reply #1 on: February 14, 2024, 03:58:35 AM »
There is no need for eval, you can return the data by simply placing the variable as the last evaluated statement in the function definition, e.g.:

Code - Auto/Visual Lisp: [Select]
  1. (defun foo ( / myList n )
  2.     (repeat (setq n 10)
  3.         (setq myList (cons n myList)
  4.                    n (1- n)
  5.         )
  6.     )
  7.     myList
  8. )

keithsCADservices

  • Newt
  • Posts: 45
Re: Best way to create return values from a function
« Reply #2 on: February 15, 2024, 09:53:56 PM »
So obvious yet I never caught on!

And this will return the symbol or the value? I guess I can just try it out for myself!

But... I'm not changing my code even if it looks dumb... I'll just say it's my signature ;-)

Thanks Lee!

JohnK

  • Administrator
  • Seagull
  • Posts: 10646
Re: Best way to create return values from a function
« Reply #3 on: February 15, 2024, 10:05:54 PM »
Interesting question. Can you give an example? What and How you return is fixed but are you asking When? -i.e. as in, when you break from a loop in C/C++/C#.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

keithsCADservices

  • Newt
  • Posts: 45
Re: Best way to create return values from a function
« Reply #4 on: February 16, 2024, 01:29:25 AM »
I could give an example but I need to think of one that answers what you're addressing. I suspect that my question was so basic that it threw people off lol.

Basically, I just need my functions to return the variable I want. It was so common for the last statement in my functions to return the result I was looking for that I was just lost as to how to make my function return something that wasn't the last returned item. Maybe a bad example: But imagine you're processing a list of numbers and trying to find the highest number. After the highest number is found each item comparison then returns nil... so in that case the function would then return nil. An ugly way to deal with this would be storing that highest number in a global variable... but why. I found out that if I store the value in a local variable, then call it with either "setq" or "eval" in the last statement of the function (after the loop), I would be able to get the function to return the item I wanted... but it felt like an extra unnecessary step (and I never see anyone else doing it so suspected it's weird).

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Best way to create return values from a function
« Reply #5 on: February 16, 2024, 05:01:28 AM »
And this will return the symbol or the value?

The value; the symbol will be evaluated to yield its value as the last statement of the function definition. To return the symbol instead of the value, you would quote the symbol as the last statement; however, note that such symbol would hold no value outside of the scope of the function if declared local to the function.

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Best way to create return values from a function
« Reply #6 on: February 16, 2024, 05:03:49 AM »
Maybe a bad example: But imagine you're processing a list of numbers and trying to find the highest number. After the highest number is found each item comparison then returns nil... so in that case the function would then return nil. An ugly way to deal with this would be storing that highest number in a global variable... but why. I found out that if I store the value in a local variable, then call it with either "setq" or "eval" in the last statement of the function (after the loop), I would be able to get the function to return the item I wanted... but it felt like an extra unnecessary step (and I never see anyone else doing it so suspected it's weird).

For example -
Code - Auto/Visual Lisp: [Select]
  1. (defun largestnumber ( lst / rtn )
  2.     (setq rtn (car lst))
  3.     (foreach num (cdr lst)
  4.         (if (< rtn num)
  5.             (setq rtn num)
  6.         )
  7.     )
  8.     rtn
  9. )

Aside from the obvious alternative approach -
Code - Auto/Visual Lisp: [Select]
  1. (defun largestnumber ( lst )
  2.     (apply 'max lst)
  3. )

keithsCADservices

  • Newt
  • Posts: 45
Re: Best way to create return values from a function
« Reply #7 on: February 18, 2024, 08:32:38 PM »
Thanks again Lee! Learnt a few extra things as well.