Scilab Programming Guidelines

Here are some useful guidelines for programming in Scilab:


Begin each Scilab script with a comment giving a short description of what the script does.

// This script accepts a pair of numbers from the user and
// passes them as the input variables 'm' and 'n' to the
// function euclid(). The function then determines the greatest
// common divisor of the pair and returns the value as the
// output variable 'gcdiv'.

function gcdiv=euclid(m,n)
    r=modulo(m,n);
    if r==0, gcdiv=n
    else
    m=n, n=r;
    printf('m is %f and n is %f\n', m, n); // displays the values of m and n during each iteration
    gcdiv=euclid(m,n);
    end
endfunction

m=input("Enter the first of your numbers: ")
n=input("Enter the second of your numbers: ")

gcdiv=euclid(m,n)
disp("Your Greatest Common Divisor:"), disp(gcdiv); // displays the final value

Put the solutions for each question in different script files. Name the script file appropriately. If the above script is a solution to Question 5, name the script file answer5.sce.


Place the script files for each project in its own directory. If the project has several sub-projects, create a new directory for each sub-project.

For example, if you are coding all examples in a particular textbook in Scilab, create a new directory for this specific project. Within this directory, sub-directories can be created for each chapter.


When emailing someone several script files, or an entire directory, avoid attaching each file individually to the email. Send the directory as a zipped archive or a tarball.


Use the .sce extension for Scilab scripts and the .sci extension for function definitions. These are conventions used to help people identify which files contain scripts and which ones contain only function definitions without having to open the files.


Use a good text editor- preferably one with syntax highlighting. Scilab's in-built text editor highlights using the Scilab syntax. Compare this snippet of code:

function y = fibonacci(n)

// Returns the nth fibonacci number

if n == 0 then
y = 0;
elseif n == 1 then
y = 1;
else
y = fibonacci(n-1) + fibonacci(n-2);
end

endfunction

to this:

Syntax highlighting


Indent your code:

Indenting your code can help in understanding the flow of control of the code and where various blocks of code belong. Using white space appropriately can help one locate and resolve errors faster. Several text editors can indent your code automatically. Check the documentation of your favourite text editor. Compare this snippet of code:

function y = fibonacci(n)
// Returns the nth fibonacci number
if n == 0 then y = 0;
elseif n == 1 then y = 1;
else y = fibonacci(n-1) + fibonacci(n-2);
end
endfunction

to this:

function y = fibonacci(n)

// Returns the nth fibonacci number

    if n == 0 then
        y = 0;
    elseif n == 1 then
        y = 1;
    else
        y = fibonacci(n-1) + fibonacci(n-2);
    end

endfunction

Use descriptive variable and function names.

This prevents one from using the same variable or function name inadvertently for unrelated purposes. It also helps one understand the code faster when reading through it.

Compare this snippet of code:

if a < 24 & b < 60 & c < 60 then
    disp("The time is right");
else
    disp("The time is not right");
end

to this

if hours < 24 & minutes < 60 & seconds < 60 then
    disp("The time is right");
else
    disp("The time is not right");
end

Use functions for tasks that repeat themselves.

Dont store all your functions in a single file- Loading or recompiling a file full of functions wastes computing resources if you only have to use a very small set of those functions. However, one should save closely related functions, or functions that are often used together, in the same file- it saves one the time of having to search for related functions when one has been found.


Comment your functions and scripts

One should comment ones functions and scripts well so that one is able to determine what it does without having to go through them line by line. Function definitions should ideally have a comment following the function declaration describing the function in short.

function y = euclid(m,n)
    r = modulo(m,n);
    if r == 0 then y = n
    else
    m = n, n = r;
    y = euclid(m,n);
    end
endfunction

function y = euclid(m,n)
// Determines the Greatest Common Divisor of a pair of numbers m and n using the recursive Euclidean algorithm
    r = modulo(m,n);
    if r == 0 then y = n
    else
    m = n, n = r;
    y = euclid(m,n);
    end
endfunction

It is not useful to have comments for every single line of your function or script- particularly when the code on the line is straightforward. When the code is not very obvious, or if a particular line of code implements something in an unexpected manner, a comment should point it out and provide a short explanation.


It is also useful to have a comment at the beginning of each script identifying the program, the programmer, the project name, the version number and so on.

// Lissajous Curves. Version: 0.7
//
// Analogue Electronics Project
//
// Author: Aditya Sengupta (sengupta@ee.iitb.ac.in)
//
// This file contains a set of functions used to simulate the generation of
// Lissajous curves on an Oscilloscope. It is a graph of x versus y, where x
// and y are respectively:
//
//     x = Asin(at+p)
//     y = Bsin(bt)