✉️ User functions definition – the unique convenience provided by the Scalar Calculator

Functions for mathematics are what elementary particles are for physics. For this reason, the Scalar Calculator and its mathematical engine, provide the syntax for defining user functions, which is as close to natural as possible. Just enter f (x) = x^2 and you are ready to go. Stay tuned to learn a bit more details! 🙂

⭐ Basic user defined functions

The syntax for defining user functions is as follows.

FunctionName(param1 <, param2, …>) = expression

It is much easier to understand this on the basis of an example – it is really easy. Please note that you can easily define a function depending on the function you have already defined. It gives you great flexibility.

Scalar Calculator - User Functions

Scalar code result:

scalar > f(x) = sqrt(x)
scalar > g(x) = f(x)^2
scalar > h(x) = g(x)^2
scalar > h(4)
e1 = 16
scalar > g(4)
e2 = 4
scalar > f(4)
e3 = 2
scalar > Showing command history

Scalar script:

f(x) = sqrt(x)
g(x) = f(x)^2
h(x) = g(x)^2
h(4)
g(4)
f(4)

I am sure, that from time to time, you will want to check what you have already defined. This is possible using context help. Long click on the “example /?” button, then select the user items option.

Scalar Calculator - User Functions - Help

⭐ Drawing charts of user defined functions

Each defined function is available in the Scalar namespace. Nothing prevents you from referring to these names anywhere in the application. In particular, it allows you to draw graphs of your own functions.

Scalar Calculator - User Functions - Chart

⭐ User defined functions with many parameters

Nothing special in this case as Scalar has no restrictions. Simply use the similar syntax to the presented in the below example.

Scalar Calculator - User Functions - More parameters

Scalar code result:

scalar > f(x,y) = sin(x) + cos(y)
scalar > f(pi/6, pi/4)
e1 = 1.2071067811865475
scalar > 1/2 + sqrt(2)/2
e2 = 1.2071067811865475
scalar > f(x,y,z) = x*y*z
scalar > f(1,2,3)
e3 = 6
scalar > f(4,5,f(1,2,3))
e4 = 120

Scalar script:

f(x,y) = sin(x) + cos(y)
f(pi/6, pi/4)
1/2 + sqrt(2)/2
f(x,y,z) = x*y*z
f(1,2,3)
f(4,5,f(1,2,3))

⭐ Variadic user defined functions

Scalar provides a special syntax for defining functions with any (variable) number of parameters.

FunctionName(…) = expression

“(…)” is obligatory as it indicates that you create function with variable number of parameters.

Additionally Scalar provides special keywords that help to build expression defining variadic functions.

  • [npar] – number of parameters provided by the user on calculation request
  • par(i) – value of parameter at index “i”

Please refer to the below example

Scalar Calculator - User Functions - Variadic

Scalar code result:

scalar > f(...) = [npar]
scalar > f(2)
e1 = 1
scalar > f(3,1)
e2 = 2
scalar > f(4,2,5,6)
e3 = 4
scalar > f(...) = sum(i,1,[npar],par(i))
scalar > f(1,2,3,4,5)
e4 = 15
scalar > 1+2+3+4+5
e5 = 15

Scalar script:

f(...) = [npar]
f(2)
f(3,1)
f(4,2,5,6)
f(...) = sum(i,1,[npar],par(i))
f(1,2,3,4,5)
1+2+3+4+5

Thank you for your time 🙂

All the bets!

? Scalar in action

? Scalar Lite (Free)

Get it on Google Play

? Scalar Pro

Get it on Google Play

Leave a Reply

Your email address will not be published. Required fields are marked *

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close