How to call a function inside another function (and use the returned constant)

Hi all.

Ive got some chialisp where I’m trying to calculate a value which changes based on the solution.

This then gets passed to another function which uses it. A simplified version would go something like this:

(mod
(
; define arguments
my_amount
puzzhash_to_create
)

(defconstant THRESHOLD_AMOUNT 10)
(defconstant BIGGER 2)
(defconstant SMALLER 1)

(defun-inline Calculate (amt)
     (if (> amt THRESHOLD_AMOUNT)
          (BIGGER)
          (SMALLER)
     )
 )

(defun-inline conditions_func (puzhash amt)
     (list CREATE_COIN puzhash (Calculate amt))
 )

(conditions_func puzzhash_to_create my_amount)

However, this is giving me some errors, and i’m not sure if what i’m doing is me misunderstanding the language. What i’m trying to do is use the first function to return a value inside of the second function. If possible i’d like to clarify the following points:

  1. when to use defun-inline or just use defun - are they both globally scoped, or do I need to pass one function to the other in the arguments to use it?

  2. are the constants globally scoped?

  3. Can anyone give me some pointers why this code doesn’t work - can I call one function in the other function in such a way?

Thanks for all your help! Partial answers to some of these questions also appreciated! - Thanks

Dan

Both are globally scope. Basically, defun-inline is more efficient, but if you need to create a recursive function, you’ll have to use defun. There is a good note here and here.

In most instances, it is better to use inline functions rather than regular functions. Inline functions get inserted where they are called at compile time which will eliminate the function call overhead and will not store the function separately in the code.

There is a potential scenario where this is not true. If you are using an inline function with an argument that has been calculated, you will end up paying for that calculation every time the argument is referenced:

yes

There are couple things that needs to be fixed. For example, you will need to include condition_codes.clib as well. And in calculate function, since you want to return the value and not running another function, you don’t need (). The code below should work:

(mod
    (
        ; define arguments
        my_amount
        puzzhash_to_create
    )
    (include condition_codes.clib)

    (defconstant THRESHOLD_AMOUNT 10)
    (defconstant BIGGER 2)
    (defconstant SMALLER 1)

    (defun-inline calculate (amt)
        (if (> amt THRESHOLD_AMOUNT)
            BIGGER
            SMALLER
        )
    )

    (defun-inline conditions_func (puzhash amt)
        (list CREATE_COIN puzhash (calculate amt))
    )

    (conditions_func puzzhash_to_create my_amount)
)
❯ brun $(run ./test.clsp -i .) '(20 0xdeadbeef)'
(51 0xdeadbeef 2)

Thankyou! You solved this for me → I was returning the constants in brackets (which i guess was treating them more like conditions rather than a value!) Thanks!