display, the value is not one or two.
Okay, so that's a if-else
if-else statement it ends.
And of course, here we've just
set v 1 equals 2, so hopefully, yup,
displays that the value is 2.
And finally, I don't
think I talked about this earlier, but
if you ever need to exit Octave,
you can type the exit command and
you hit enter that will cause Octave
to quit or the 'q'--quits
command also works.
Finally, let's talk about
functions and how to define
them and how to use them.
Here's my desktop, and I
have predefined a file
or pre-saved on my desktop a file called "squarethisnumber.m".
This is how you define functions in Octave.
You create a file called, you know,
with your function name and then ending in .m,
and when Octave finds
this file, it knows that this
where it should look for the definition of the function "squarethisnumber.m".
Let's open up this file.
Notice that I'm using the
Microsoft program Wordpad to open up this file.
I just want to encourage you, if
your using Microsoft Windows, to
use Wordpad rather than
Notepad to open up these
files, if you have a
different text editor that's fine
too, but notepad sometimes messes up the spacing.
If you only have Notepad, that should
work too, that could work
too, but if you
have Wordpad as well, I
would rather use that or some
other text editor, if you have a different text editor for editing your functions.
So, here's how you define the function in Octave.
Let me just zoom in a little bit.
And this file has just three lines in it.
The first line says function Y equals square root
number of X, this tells
Octave that I'm gonna return
the value Y, I'm gonna
return one value and that
the value is going to
be saved in the variable Y
and moreover, it tells Octave
that this function has one argument,
which is the argument X,
and the way the function
body is defined, if Y equals X squared.
So, let's try to call
this function "square", this number
5, and this actually
isn't going to work, and
Octave says square this number it's undefined.
That's because Octave doesn't know where to find this file.
So as usual, let's use PWD,
or not in my directory,
so let's see this c:\users\ang\desktop.
That's where my desktop is.
Oops, a little typo there.
Users ANG desktop
and if I now type square
root number 5, it returns the
answer 25.
As kind of an advanced feature, this
is only for those of you
that know what the term search path means.
But so if you
want to modify the Octave
search path and you
could, you just think of
this next part as advanced
or optional material.
Only for those who are either
familiar with the concepts of
search paths and permit languages,
but you can use the
term addpath, safety colon,
slash users/ANG/desktop to
add that directory to the
Octave search path so that
even if you know, go to
some other directory I can
still, Octave still knows
to look in the users ANG
desktop directory for functions
so that even though I'm in
a different directory now, it still
knows where to find the square this number function.
Okay?
But if you're not familiar
with the concept of search path, don't worry
about it.
Just make sure as you use
the CD command to go to
the directory of your function before
you run it and that actually works just fine.
One concept that Octave has
that many other programming
languages don't is that it
can also let you define
functions that return multiple values or multiple arguments.
So here's an example of that.
Define the function called square
and cube this number X
and what this says is this
function returns 2 values, y1 and y2.
When I set down, this
follows, y1 is squared, y2 is execute.
And what this does is this really returns 2 numbers.
So, some of you depending
on what programming language you use,
if you're familiar with, you know, CC++ your offer.
Often, we think of the function as return in just one value.
But just so the syntax in Octave
that should return multiple values.