So we know a crash course we have encountered already several functions that are built in either Python
or Nampai.
And here I want to show you how you can define your own functions.
So to be honest, using functions is never really necessary, but it is often very, very helpful to
structure your code to make it easier to understand what's going on and just to make the code much shorter,
especially if you try to do the same thing over and over.
It's a very good idea to define a function.
So as an example, we will later on define our own dot product function so that this would have been
very helpful if this function would not have been implemented already into Nampai.
So, but first of all, I want to show you in general how a function works.
So the main command for defining a function is deaf.
Then you write a name, you can choose whatever name you want.
And then in the brackets, you can provide arguments.
So I say you can because this is not really necessary.
And in our first example, I will define a function without any arguments.
And so I write death and I give it a name and the name will be say hello.
And as I said, it will not give any arguments.
So just write the brackets open and close and then we can go ahead with the actual commands.
And the only thing that I want this function to do is just print something like, Hello.
OK, so we run this and nothing happens.
This is because we have just defined the function.
We did not call the function yet to call the function, we just write, say hello.
So if I don't write the brackets, it will not work because we need the brackets, but we don't need
any arguments.
So I write say hello brackets and then the output will be hello, as we have defined.
So let's see what happens if I provide some argument.
There will be an error because there are zero positional arguments, but one argument was given.
So it just works, really if you don't provide any arguments in this case.
So the next thing that we are going to do is we are going to create our own dot product function.
So let me redefine the two vectors from before, so we had an array.
So this is a good exercise for you to remember how to define an array.
And we choose just some random numbers and another vector, a vector to with some other numbers.
Yeah, it doesn't really matter, but let's just two, three and four.
And the function that was built into Nampai was NPR Dot.
And then the two arguments our vector one and factor two and the Dot product is nine.
So now we want to define our own function.
And we want to get, of course, the same result.
So what I do is I write def and then I give it a proper name and I just write dot product.
So now we need two arguments.
So I could have caused right vector one Vector two, but it doesn't really matter because these are
like some dummy variables.
So I can write, for example, A and B.
So now only within this definition, A and B are defined as the two arguments.
And they do not have to be the same variable as you're going to use later on.
So now we have to define some value.
But we are going to calculate I call this C, which was really the value of the DOT product.
And to calculate this, I just make it really, really simple.
I just write the first argument or the first component of a times B and then plus I take the Y coordinates
and the Z coordinate, so I just have to make sure these indices here are correct zero, one and two.
So now this is how you calculate the dot product.
Of course, you can make this a much, much nicer by yeah, using some some function, for example.
Or also you can generalize this.
For example, in our case, I have assumed that the A and B vectors have three components.
But of course, you can also define a dot product for vectors which only have two components or even
more components.
But here we want to keep it very simple and just stick to the three component vectors.
So now if we would run this, it would not work yet because the only thing it does is it would calculate
the value of C and it wouldn't do anything with it.
So just for testing purposes, we can write print C.
And now if I make a new cell and I call our new function dot product and now I, of course write our
two vectors, then it will give me the answer.
Nine.
However, here's a problem that we have.
So if we only want to output the number, then this would be OK.
But what if we want to define some variable and assign this value to it as variable?
So then I would write a D as equal to dot product vector one Vector two.
So far, it seems as if it would have worked.
So let's call again the value of D, and you see nothing happens.
So the problem is we have to find that we will print the value of C.
So what you see here is not the value that is assigned to DH.
It's just this print command so that this is a bit tricky.
So if you want to really work with the result, which of course we want, then using print is not a
good idea.
So I use a comment here so that this line doesn't do anything anymore.
And I write here, it's not really useful.
And instead, what do you have to do is you have to write a return return.
See?
So now let's redefine it.
Let's calculate this.
So do you see this time we do not get an output because we did not specify to print anything.
But internally, the value of the product will now be the value of C because we wrote the return C.
And so now we have a scientist value to D and we can call the and it works.
And now, of course, we can also continue to calculate something here.
All right.
So you have seen how to create a function without any arguments and you have seen how to create functions
with arguments.
Now the last thing that I want to show you is how you can use the so-called argument keyword and for
this?
Let me show you an example.
So once again, I define a function and I call this function personal data.
And for the arguments, I use some.
No, so this will become clear in a second.
Name and age, so it should be something like a database of some of these properties of a person.
So, of course, would be a good idea to use a dictionary, but here instead, I want to do something
else.
You will see in a second what I'm going to do, so I will write here.
What the function will do is it will print several things.
So the first thing it will print will be the idea.
So the the number basically like some number in a database or on your passport, you have some number.
So it will print this idea and we merge these two a string.
So we will merge ID and then the space here and then we will turn the number into history.
Then it will print another line, which will be the name.
So once again, we marched a string with another string.
And this time name is typically a strings, or we don't have to convert to type here.
And then the last thing will be to print the H.
So we write Print H merge to string with string h because no, an h o no and h are a numbers.
So we have to convert them to strings first to be able to merge them with the other string.
So now we have to find this function and now let me call the function.
So I write personal data and now I have to provide the arguments.
So let's not 449.
This would be a bit unrealistic.
So let's honor this was the number.
So it would have been fine.
So let's just do it.
Number is 449.
Then the name is, I don't know, Peter and H is twenty nine.
So if we run this, this will be the result.
And of course, if I would have made a mistake here and would have brought here 29 and here 149 down
the ages 449.
All right.
So let's get back to what I wanted to do initially, which is this one.
So now imagine we have here many, many arguments and many, many lines of codes.
And so it can be a bit tricky to remember all of the the order of these arguments and you have to check
every time.
So what we can do instead is we can use the keyboard.
Yeah, the argument keywords.
And so what we're going to do is we're going to write you.
Number is equal to 49.
Name as equal to Peter and H is equal to 29.
Will give exactly the same result.
However, the nice thing about this is that these arguments do not have to be provided in the correct
order anymore so we can do something like this and it still works and it gives us the correct results.
So I think and most of the cases, we will not really use the argument keywords, but it's still a good
to keep this in mind that something like this exists because it can make our lives sometimes a bit more
easy.