How to call function in a note

I ‘created’ a addTax function in note /Hints/Library

function addTax(price) { return(1.18*price);}

Then I created an attribute Test2. The $Test2 is set as number.

I created a note in the ‘root’ directory. I then added in the Rule:

$Test=addTax(5);

However, the $Test remains 0.

Where am I going wrong?

Thank you!

*souced from a webpage

Is $Test a user attribute? Or only $Test2?

I ‘created’ an addTax function in note /Hints/Library

function addTax(price) { return(1.18*price);}

Then I created an attribute Test2. The $Test2 is set as number.

I created a note in the ‘root’ directory. I then added in the Rule:

$Test2=addTax(5);

However, the $Test2 remains 0.

Where am I going wrong?

Thank you!

*sourced from a webpage

Sorry! Typo. It is $Test2

It is $Test2 (rather than $Test).

Seems to work for me: test price-test.tbx (160.3 KB) based on the info above. The test note is ‘zz’. The function is in library ‘tax test’. The addTex() code worked as typed but I tidied the formatting of it up a bit for clarity.

1 Like

Funny, I just created a demo too.

@naren17, see attached, there are several element to adjust.

FunctionTest.tbx (180.1 KB)

@mwra and @satikusala, Thank you for your example files. I reproduced your files and got them working.

I dug to find my mistake. I pinpointed the problem: I had not left a space after the “return.”

Cheers!

1 Like

Thanks. For later readers, return is one of the few action code operators that doesn’t take parentheses after it. Accidentally writing the function in this form:

function addTax(price) { return(1.18*price);}

makes the app’s parser—the app reading/interpreting that code—think that the returning statement is in fact an—illegal—input argument for the return operator. The minimum fix required is to insert a single space after return:

function addTax(price) { return (1.18*price);}

This now indicates parentheses around the price calculation and signals to the parser—if necessary—to multiply price before returning the result.

A more customary—but not required—method of writing such a function would be:

function addTax(price){
   return (1.18*price);
}

The changes are essentially ‘white space’. They don’t alter the process but may add clarity, especially those new to action code and/or to coding more generally.

†. When I wrote earlier I’d tested the function ‘as is’, I must have reflexively added the missing space without registering that I had done so. As a TBX is not some high-volume/speed process, writing code in minimal space if often a vector for error. A few slices or line breaks can do much to aid oneself when getting code to work.

1 Like

@mwra Thank you! Adding a space after return sorted the problem!

1 Like