## Sunday, October 2, 2011

### Using a Scilab Function

When you begin learning Scilab, the first task to accomplish is to learn the list of available functions, what they do and how to use them.

In this post I attempt to show how to use an existing Scilab function. Scilab has a large library of functions for various applications. For example, sqrt() calculates the square root of a number, det() calculates the determinant of a matrix.

Find the name of the function you need
The first task is to find if Scilab has a function available to to do something you intend to do. To find an answer to this question, either use the Scilab help or ask a friend who knows Scilab.

Tip: Scilab help is accessible from the Scilab console, on the main menu (? at the right end) and choose Scilab Help. But there is a short cut, press the F1 key on the keyboard.

In the Scilab Help Browser (Scilab online help is hyperlinked and Scilab help browser helps you navigate the help documentation), choose the Search tab and type a word pattern related to the function you are searching. For example, type the word determinant if you want to find a function that calculates the determinant of a matrix. The sidebar on the left of the Scilab help browser shows all items in the help documentation containing the word(s) you typed. Search the list of items in this list and you are likely to find the function you are looking for. If not, refine your search.

Tip: An easier way to do the same as above, is to type the following command in the Scilab console:
-->help determinant

Learn how to use a Scilab function
Having found the function you need, study the Scilab help page for the function. Help on each function will give the following information about the function:

1. Name and brief description: Name of the function and a brief description of its purpose
2. Calling sequence: Different ways in which the function can be called. There may be multiple ways in which the function can be called, with different number of input and/or output arguments
3. Arguments: List and description of input and output arguments
4. Description: A detailed description of the function and what it does
5. References: Some technical information about the function
6. Examples: Short example(s) showing how the function can be used
7. See Also: List of other Scilab functions that are related to the function you are studying
Try out the function
It is now time to use the function. First prepare the input that the function requires, if you don't already have it. If you want to calculate the determinant of a matrix, first define the matrix. Let us define a 3x3 matrix, with the name a, by typing the following at the Scilab console:

-->a = [3 2 1; 2 4 6; 3 3 5];

Let us now find the determinant of the matrix a and store the result in d.

-->d = det(a)
a  =
16

There we have the answer. Determinant of the matrix a is 16, and the result is stored in d for subsequent use.

Summary
To use a Scilab function:
1. Find the name of the function that you need. Use Scilab help
2. Study the help documentation. Read the description to verify that the function does what you intend to do. Note what input data it requires and what results it returns
3. Prepare the input data and call the function
Here are some useful tips:
1. Study the list of functions related to the function you are currently studying at the bottom of the help documentation under See Also. This way you can quickly enlarge the portfolio of Scilab functions that you know
2. Some Scilab functions return multiple results. By default you will see only the first, unless you provide sufficient number of output arguments (enclosed within square brackets on the left of the assignment operator, namely, =) to retrieve the other results
3. If you do not supply any output argument (that is, no assignment operator and nothing on the left hand side, simply an expression), Scilab stores the first result returned by the function in a special variable named ans.
The above discussion applies not just to Scilab, but also to any other programming language. Thus:
1. Each programming language comes with its standard library of built-in functions and the beginning programmer has to discover the functions that (s)he needs to use by studying the documentation for the programming language from traditional sources such as books or modern sources such as Internet search engines
2. Some programming languages categorize functions as functions and procedures/subroutines. Functions can be used as part of expressions whereas procedures/subroutines can only be called but not used as part of an expression. Returning multiple results is handled differently by different languages. Python is similar to Scilab while C/C++, Fortran, PHP use a different approach. Especially when an argument is an array, be aware how the programming language treats it
3. Every programming language follows the semantics of a parent function calling a child function. Data to be sent into a function is first stored in a variable in the parent function, sent into the function as an input argument. Parent function waits until the child function completes its task. When the child function completes its task, parent function receives the results returned by the function and stires them in variables if required for subsequent use