In this lesson we will talk about functions in Scilab.Functions are an important part
of any programming language and there are many words or names used for essentially the
same thing which are functions such as some programming languages call them subroutines
they are also called procedures or macros or subprograms so all these keywords are essentially
for the same purpose so what functions do?
so functions are basically a piece of code which we dont want to write again and again
in larger [programs so we write that piece of code once and then we call it in our main
programs whenever we need and we send some variables to this code and we get back certain
variables from that code so this is what the functions do so this piece of code is called
function so essentially the functions are just a piece of code where we send certain
variables and we manipulate those variables and then we get back whatever we want some
of the examples could be like we send two points to a function the coordinates of two
points and we get back the distance between these two points so this could be a function
that we send ok this is our point x1, y1 and there is another point x2,y2 and we send these
four coordinates to this piece of code and we get back the distance between these two
points similarly another example could be that there is a rectangle and we have two
sides for this this is a side a and this is side b and we send these two sides data these
two variables to this function and we get back the area of the triangle and as well
as the parameter of the rectangle so you can get one value from a function you can get
more than one values from the function so every language has certain built-in functions
similarly Scilab also has a lot of builtin functions like square root, sine, cos and
tan the trigonometric functions and what you give here is you give some variable any value
for example x and you get the square root of that value back similarly these are the
trigonometric functions very popular you can get the maximum of any vector or any matrix
you can also get the absolute value the positive value if you give the negative value here
you can get back the positive value similarly inverse function is very common you can use
this to find the inverse of a matrix or inverse of a single number and there are many more
functions you cannot count there are hundreds of functions in Scilab No we will make two
functions in Scilab so the first function will take the coordinates of two points p1
and p2 so these four values x1,y1 and x2,y2 we will send these values to the function
and the function will return us the distance between these two points and the formula of
the distance as you already should know that formula of the distance is x1-x2 whole square
plus y1 minus y2 whole square and this under root so this is
the distance formula so the function will calculate the distance and will return back
the distance the second function which we will be making here because this function
will return two values so you will know how to get one value from a function or two values
from a function so in this case we will send two sides of a rectangle to this function
and it will calculate the area by multiplying the two sides so side 1 multiplied by side
2 and it will also calculate the paremeter which is basically side 1 + side 2 this is
the formula for the paremeter of the rec tangle multiplied by two so the function will calculate
these two values and will return us these two values so this is what we are going to
code next so now let us make these functions so first of all we have to make the function
for distance between two points so we start our editor Scinotes and we start with the
keyword function function is the keyword and as soon as you press this space the editor
will show you this pare of paranthesis and the word end function end function is the
last word of your function if you are returning only one value you can just write any name
of the variable for example a if you are returning more than one values then you have to put
square brackets like this inside this and its a good practice to use these square brackets
anyway even if its not required for one variable but you still use it then you say equals and
then you put the name of the function for example I want to write this function as capital
distance in these parenthesis you have to put name of the variables and the number of
variables should be the same which you want to send to the function so in this case we
will be sending four variables x and y coordinates of two points so x1, y1, x2, y2 so you should
select these names in a way that if someone else is looking at your code they can guess
what these values might be so its a good practice but you can put any name here it does not
matter now we have to calculate this variable a or you can call it d because d is more appropriate
for distance d equals and now the formula sqrt and then you start the parenthesis and
within these parenthesis we have x1 minus x2 whole square means hat 2 plus y1 minus
y2 hat 2 so this is the formula for distance and then a semicolon and your function is
ready this is the function very simple normally we use functions to save our time when there
is quite a bunch of code like 50 or 100 lines which we dont want to write again and again
so we write functions for that but essentially you can put as many lines between function
and end function as you like this is just to give you how what is the syntax of a function
in Scilab because every language has a little bit different syntax so here the key points
are that here first of all you have to write how many variables you want to return in this
square brackets then equal sign and then name of the function dont put equal after the
name equal should be before the name of the function because we will be calling the function
in this way distance and then we will be sending these four points and then whatever value
is here that you will be calculating here so this you want to return back this d will
be returned if you calculate something else here some other name then it will not be returned
this is the syntax so now we have to save this first and then we have to execute this
function so we save it save and whatever is the folder you have to take note of this folder
because when you execute you have to give the full path of the file so I give it the
name distance you can put any name of the file but I will just keep the same name so
distance and then as you can see extension is .sce but this is only recommended this
is not compulsory you can also use .sc or .sci as well and then you just save it and
close this in order to execute you have to give the full path upto the function file
name with a command exec and then you start ' and then c: this is my path users and then
the name of the function file and then ' close and then enter so if you see function and
your function command and then end function then it means there is no error or there is
no warning so sometimes you get warnings when this function is already present and you try
to redefine this function then you get a warning here so in that case you should have a look
that weather you really want to redefine or there e is a some problem or otherwise if
there is no error then it means that the function ahs been successfully compiled now you can
use this function however you like the only thing here is that the variable you are getting
back should be here in your matrix for example i can say I can keep a,b any variable lets
say a equals and I should be giving the function name and the same number of variables so distance
and then so in order to check I should first give a simple point first point 0,0 is I think
fine at the origin and my second point is 10 unites on the x-axis and zero unit on the
y-axis so clearly the distance should be 10 so see whether program gives us 10 or not
so as you can see that it gives us correct distance now you can check with another simple
point and that could be for example from origin three units on the x and 4 units on the y
because we know that if we move 3 on x 4 on y then the distance should be 5 and now it
is correct so it means our function si working properly now we define the second function
so scinotes once again and you can cancel this because it is asking for the same function
and now the second function is that we are sending two sides of a rectangle and we are
getting two values so again we start with the function and as soon you press space it
gives you these two things now lets say the program name is AP. AP means area parameter
and the values we getting are two so therefore we have to put two variables here so lets
say A and P here we will be giving side1 and side2 two sides and then we have to put an
equal sign before the function name here and then we can calculate A and P A will be equal
to the area side1 multiplied by side2 semicolon and the parameter will be equal to 2 multiplied
by side1 + side 2 so just have a look once again semicolon here parenthesis are correct
and this multiplication sign should be there everything looks fine so I just save this
save so now I can put a different name instead of AP i put AreaP the name of the function
AreaP so save it close it and now just put the name of the function which is AreaP we
enter and again all commands were executed successfully so it means we can use the function
and this function is returning two values so we have to put two variables something
like give me area and give me parameter you can take any variable name equals then we
call the function and what was the function name function name is this AP not the file
name the function name so AP and then we give two values lets say 4 and 5 and enter so now
we got this error so why we got this error because the p the function can not find any
variable p now here is the error as you can see here that we should return two variable
a and p but we are returning only the variable a so therefore function is compiled successfully
but when we try to get the values from the function the function is saying undefined
variable p because function when try to return the value p function can not find any variable
p inside the code so it means this variable should be p because there is no p inside the
function so therefore we got this error so now you can save again and now as you can
see because we have defined this function before so it is showing us the warning so
this is the warning I was talking about before that if the function is already defined it
will only show you the warning that you are redefining the function so is it ok? this
is what you want? so there is no problem there is no error only the warning so now we can
execute it again with the same variables give me A and give me P and if I enter it gives
us the paremeter and the area so this is how you use the functions and these are the warnings
and some common errors which you will get when you will try to make functions so the
definition of every variable you are returning must be inside the code of the function so
that is it for the functions now you can define any function the number of inputs and the
number of outputs should be proper and all variables which are outputting from the function
must be defined insidie the function