NeturalMath Quick Start Guide

Part 7: Using System Functions

This document is not complete, and my represent features which are not yet implemented in the current version of NeturalMath.

By combining operators and symbols into expressions, we are able to represent a vast range of mathematical behaviors. Constants and variables allow us to solve a large range of mathematical problems, but not everything. As you may have noticed, we still do not have a good way to represent certain mathematical concepts common to science and engineering, such as trigonometric functions or logarithms. It turns out that NeturalMath has an easy way to make use of these concepts. The are called functions. Functions are used for two reasons. The first is to perform mathematical calculations which are common, but would be difficult to program over and over in NeturalMath. These include things such as logarithms, sine and cosine calculations. The second reason is to make it easier to call a behavior (or set of behaviors) repeatedly. Another way to look at this is that functions simplify programs by providing reusable behaviors.

Some functions are simple and purely mathematical in origin. These would be something like the sine function in trigonometry, as shown below:
// may be used alone
sin(10)

// or as part of an expression
x = sin(10)  // use with constants
y = cos(z)  // or variables
z = tan(13) + 11 // may be part of a larger expression


Other functions perform some behavior. The reverse function, which reverses the order of a set or text, is a good example of this (working with sets and text will be covered in a later topic):
s = 'NeturalMath'
r = reverse(s)
print r  // prints 'thaMlaruteN'


A third class of function are what is known as meta functions. These functions return information about the system, expressions, symbols, etc. These functions are most often used to test the system, debug problems or modify variables and other functions. The variables() function is a good example. When this function is called, it returns a list of all of the variable names that have been defined in the system. Try it and see the result:
print variables()

// the functions() function lists all functions in the system
print functions()

For a list of all system-defined functions, please see the System Functions Page.

Calling Functions

Functions must always be called using parentheses after the function name. Function names follow the same general rules as variable names, in that they must start with a letter and be comprised of only letters and numbers. Zero or more parameters may be placed inside of the parentheses, seperated by a comma if there are more than one. The number of parameters depends on the particular function. Some functions have none, while others have many. Some parameters of a function are optional, meaning that if no value is given, a default value will be used instead. The example below shows how to call a number of functions.
// The variables() function has no parameters
print variables()

// The sin() function takes one parameter, an angle in degrees
print sin(10)

// the ln() (natural log) function also takes one parameter
print ln(30)

// the round() function takes two parameters, one for the number to be rounded, and one for the number of places to round to
print round(10.1234,2)

// many functions also use optional parameters, such as the log() function.  
// If the second parameter is provided, the logarithm of the first parameter in that base is returned.  
// If the second parameter is not provided, it defaults to base 10.
print log(16,2)  // prints log of 16 in base 2
print log(16)  // prints log of 16 in base 10

Looking up Parameters

Calling the parameters() function and passing in the name of the function in symbolic notation will return a list of parameters for that function. See the example below:
print parameters($sin)


More information about how to use functions, as well as how to create your own custom function is provided in the next topic: Creating Custom Functions

Last edited Nov 27, 2010 at 4:10 PM by zanethorn, version 4

Comments

No comments yet.