Next: Programming in Fortran or
Up: Programming Scilab
Previous: Inline functions
Contents
We can introduce inline functions at the prompt of Scilab or within `sce' files. Although they can include several lines, their edition becomes difficult if they grow or branching of code appears. It is better to define functions in a different file that can be called by other programs too. A file containing functions usually has the extension `sci' and can contain one or several functions. To define a function, we use our text editor and write this code in the file `example.sci'
function out = halfbis (in)
// this function is a version of inline function half
out = 0.5 * in;
function out = ntimesbis (in1, in2)
// this funnction is a version of inline function ntimes
out = in1 * in2;
function [out1, out2] = twicebis (in)
// this function is a version of inline function twice
out2 = in;
out1 = [in,in];
Notice that the end of each function is marked by the word `function', which indicates the beginning of the next one, or by the end of file. The syntax of functions in files is simpler than in the case of inline functions since different lines take commands apart. When Scilab begins, the functions defined in files can not be called. They have to be read from the file with `getf'. This command affords reading all or some functions of a file. Once Scilab gets functions, they remain accessible until some other function with the same name is loaded or until it is cleared with the command `clear' like any other variable. The next code illustrates the use of the file `example.sci'
-->getf('example.sci')
-->halfbis(5)
ans =
2.5
-->ntimesbis ( [3, 6, 9], 3)
ans =
! 9. 18. 27. !
-->[o1,o2] = twicebis('well')
o2 =
well
o1 =
!well well !
Although both types of file (`sce' files and `sci' files) contain code, they are very different. `sce' files are executed while `sci' files are loaded to be used when we need them. The difference is not determined by the extension of the file but by their internal structure and the command we use to access them. In the main window of Scilab there is an item of the principal menu called `File'. If you select its first option, `File Operations', a window appears with a lot of options to act on files. The last line of buttons includes `Load' (to load Scilab variables in files created by `save'), `Getf' (to load Scilab functions) and `Exec' (to execute Scilab code just as if we introduce it at the prompt of Scilab).
When we define functions, some checking of inputs contribute to the security of code. Inside a function the command `argn(0)' returns the number of arguments of input and output with which function were called. Scilab functions admit optional parameters (in the documentation of Scilab optional arguments are enclosed by brackets) and Scilab functions introduce `default' values for the values not introduced when we call the function.
It is a good method to write what Lydia van Dijk and Christoph Spiel called `Bulletproof functions'. Basically, the steps included are
- Checking the number of inputs with `argn(0)'
- Checking the type of inputs with `type'
- Checking the structure of inputs (scalar, vector or matrix) with `size'
Next: Programming in Fortran or
Up: Programming Scilab
Previous: Inline functions
Contents
j
2003-01-23