0% found this document useful (0 votes)
136 views

6.3 Functions: Chapter 6 Functions and Loops

Functions allow programmers to reuse code by defining blocks of code that can be called when needed. In Scilab, functions can be defined inline or in separate files with a .sci extension. Functions take input arguments, perform operations, and return output. Loops can be used within functions to repeat tasks. This modular approach makes code more organized and easier to debug.

Uploaded by

Li Re
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
136 views

6.3 Functions: Chapter 6 Functions and Loops

Functions allow programmers to reuse code by defining blocks of code that can be called when needed. In Scilab, functions can be defined inline or in separate files with a .sci extension. Functions take input arguments, perform operations, and return output. Loops can be used within functions to repeat tasks. This modular approach makes code more organized and easier to debug.

Uploaded by

Li Re
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Chapter 6 Functions and 

Loops

6.3  Functions
A function is a set of codes that can be called when required. As a result,
it can be defined separately either its own file or within the body of the
program. A script file is similar in nature. A script file stores a sequence
of commands to be executed. It seems that a function and a script have a
similar nature, but, unlike MATLAB and Octave, Scilab provides separate
kinds of files for each one of them. This is based on the nature of their
behavior with the core Scilab program.
Whereas a script file (with extension .sce) is an executable file,
a function file (with extension .sci) stores a set of instructions. The
function file behaves like a black box where input is fed and output is
obtained. On the other hand, a script file changes its behavior as per input
values. Whatever input data a script file accesses is taken from the Scilab
workspace. Output data from a script file is put into the Scilab workspace.
The semantics of input data, local variables, are visible only within the
function.
The definition of a function follows this syntax:

1  function [o1,o2,...] = function_name (i1,i2,...)


2  statement_1
3  statement_2
4  ...
5  statement_n
6  endfunction

Here the function keyword defines the object types as function.


Then a set of variables is defined that this function is expected to return
(o1,o2,... signifying output1, output2,…). Next comes an = operator and
then the name of the function. In the previous case, it is function_name.
A function take inputs (i1,i2,... signifying input1, input2,...) to produce
an output according to calculation defined in its body. Then comes the
main body of the function where commands for executing the purpose of

152
Chapter 6 Functions and Loops

the function are mentioned. The last statement, endfunction, signifies the
end of the function.
For example, we can write a function to find x2 − y2 and assign it to
variable name z, as shown in Listing 6-5.

Listing 6-5.  fn1.sci

1  function y = fn1(a,b)
2  y = aˆ2−bˆ2;
3  endfunction

Notice that the extension of this code is .sci. This file must first be
loaded in a Scilab workspace. We need to provide the full path of the file to
the built-in function exec()first and then use the function by providing its
name with input arguments:

1  −−−>exec('/Users/sandeepnagar/.../fn1.sci', −1)
2  −−−>fn1(2,3)
3  ans =
4  −5.

It is good practice to define the program as a group of function files


and call them in the master program stored as a script file. This modular
approach makes it easy to experiment with the idea and also makes it
easier to debug and test the code. A function can return more than two
values as well, as shown in Listing 6-6.

Listing 6-6.  fn2.sci

1  function[y1,y2,y3] = fn2(x,y)
2  y1 = x − y;
3  y2 = x + y;
4  y3 = y − x;
5  endfunction

153
Chapter 6 Functions and Loops

This gives the following result:

1  −−−>exec('/Users/sandeepnagar/.../fn2.sci', −1)
2  −−−>[a,b,c] = fn2(2,3)
3  c =
4  1.
5  b =
6  5.
7  a =
8  −1.

Functions can incorporate loops to regulate the repetitive tasks inside


the program. For example, the factorial of a number can be calculated
using a function given in Listing 6-7.

Listing 6-7.  factorial1.sci

1  function result = factorial1(n)


2    if(n == 0)
3       result = 1;
4       return;
5    else
6      result = prod(1:n);
7    end
8  endfunction

A function named factorial1, which takes a number n as an


argument, calculates the product of the number with all its successive
numbers. When called from a Scilab command line, the function yields the
following result:

1  −−−>exec('/Users/sandeepnagar/.../factorial1.sci', −1)
2  −−−>factorial1(10)
3  ans =
4  3628800.

154
Chapter 6 Functions and Loops

5  −−−>factorial1(10e5)
6  ans =
7  Inf

6.3.1  Inline Functions


An inline function is a short function that can be defined without having
to use the function skeleton discussed previously. This is useful only when
the body of the function is short:

1  −−−>deff('[x] = mult(y,z)','x=y∗z')
2  −−−>mult(2,3)
3  ans =
4  6

The built-in function deff() is used to define another built-in


function. The first argument of deff defines the output variable (x in this
case), function name (mult), and input variables (y and z). The second
argument defines the body of the function (x=y*z). This kind of function
is defined in a .sce file, just like a command, and thus does not need
a separate loading action. Once defined, functions can be called by their
function name along with input parameters.

6.4  Summary
Defining functions is the key to modular programming. Scilab presents an
elegant way to define and use functions both inline and in separate files.
When combined with the ability to write functions inside a loop, complex
problems can be implemented in a few lines of codes. It requires an artistic
attitude while designing an algorithm where functions and loops are the
paintbrush to devise an elegant solution to a given numerical problem.

155

You might also like