Page History: User functions

Compare Page Revisions



« Older Revision - Back to Page History - Newer Revision »


Page Revision: 2010/05/23 03:16


User Functions are functions that you write yourself. They can only return a single value or a matrix.



Examples



floor function

floor function

Here's a function to round a floating point number down to the next integer smaller than or equal to the argument.



Here's a function that returns a matrix. I didn't say it was a useful function.
funny matrix function

funny matrix function




Hints



Redefining the Inputs to a Function

Shown below is a common mistake:
common mistake

common mistake


Explanation: The common mistake shown above is avoided if the function is defined first. The variables should be assigned to their desired values only after the function is defined. Smath (like Mathcad) calculates from the top-left corner towards the bottom-right corner of the worksheet; therefore, if a variable is defined and then a function is defined using that variable, Smath defines the new function using the previously assigned VALUE of the variable. Changing the variable lower on the worksheet cannot redefine the function because the function was defined higher up on the worksheet. Therefore, if the user wants to be able to edit variable values used within a function, the function must be defined first using previously UNDEFINED variables and then the variables can be defined (and redefined as many times as desired) lower down on the worksheet. See graphic below for correct usage:
correct usage

correct usage


Function Overloading

Functions can have the same name

Functions can have the same name


Reserved Words

Reserved words?

Reserved words?


Redefining SMath function - take care!

Redefining SMath function - take care!


Arguments as Scalars or Matrices

Scalar? Matrix? arguments

Scalar? Matrix? arguments


Defining Functions with 'for' Loop

Using for loop

Using for loop

ScrewTurn Wiki version 2.0.37. Some of the icons created by FamFamFam.