In this lecture, I show you how to use functions, which is an excellent way by which you can improve
your organization of the program, will designed functions make the program easier to use?
Of course.
Right.
And to maintain two functions allow you to split the logic of your problem into smaller chunks.
And we'll be using quite a few of them in the sections that follow.
Let me show you a few simple examples to help you get started with functions.
So let's say that you would like to add two numbers together.
We've already seen this type of operation many, many times earlier.
We also learned about fireboats so we can have things such as number one and number two.
Let's give it a couple of numbers and then you can say no one has two and you get an answer back.
Now we're setting the parameters of a program.
Another thing that we can do is the next level of parameterization is to create a simple function that
has a single job, and that is to add two numbers.
So to create a function, we use the death or the reserved Caywood and then we provide a name for our
function, let's call it addition.
And then inside parentheses, we provide one of zero actually or more parameters.
You could write a function that doesn't take any parameters, so it would be defined with an empty set
of parameters.
In our case, we need two numbers in order to have an addition.
So one can say in one command number two for the second parameter and to finish the definition, we
use the colon symbol inside the block of the new function to use a simple print statement which just
adds the two numbers.
Just like that, all right, now we have a function defined and to use it, we simply call it name,
I just press the case.
So code completion worked and I need to pass in the appropriate number of parameters.
If I do pass anything, I'm getting an error message.
And you can see that Python knows that we need to pass in two parameters.
Try again.
This time I'm going to pass number two and number four for these two parameters.
And there is the answer comes back.
So I called the addition function, which performed the calculation and then printed out the result.
Let's do a little variation of the functional scenarios that I use, the print function and inside the
print function, I perform the calculation.
But very often what you'll be doing is that your function will contain several steps of processing and
then in the end you will want to return the result of those several steps of processing to whoever made
the call for the function.
And that means that we need to introduce at this point the return key word.
So I'm going to redefine my additional function like this.
I'm still expecting to receive the two parameters and there's my colon symbol.
Now inside, I'm going to create a new variable called result, and in it I'm going to store the result
of the addition between the number one and the number two.
And then once the result is calculated and ready, I'm going to use the return key word to send it back
to whoever made the call to the addition function.
I'm just saying return result and that's it.
Now, when I call the function, just call it without the print statement, I just kind of hit the up
error actually to go.
That's work.
So I'm just going to type in the edition.
Name of the function and pass a couple of numbers to say two or three.
You see that the result comes back is five.
Now, the result was because of the function call.
It was not printed by the function itself.
The result was sent back to the caller, which was this line of code right here.
And it was printed out because I'm working on the show.
If you were doing this in such a program, you would need to use the print command so it would look
like this.
And the results would come back like that.
So this is the result of the command coming back with the value that was calculated by the addition
function and then sent back to the print function by the return statement.
Another thing that you can do with functions is instead of using anonymous parameters, you can use
named parameters.
And that also gives you the opportunity to set defaults and also to make it possible for the user to
not provide a parameter if a default is present.
So I'll show you an example.
We're going to redefine addiction, but this time I'm going to have numb with a default value.
Now I've got a name for the perimeter and I've got a different value and I'm going to do a good story.
They should have been number one.
And I'm also going to have a number two.
And again, I'm going to have one as the default for number two.
And then inside I'm going to create the result variable of thing number one, two and three can result.
All right.
Now I'm going to.
Copy the print command, but I'm not going to pass any parameters and see what happens.
OK, it worked.
So the result is two, which is what one plus one gives us, it means that the addition function did
not find any use of parameters, so it just went with its default.
Another thing that you can do now, because we have defaults and we've got named Parramatta's, is that
we can go and do something like this.
And number two equals three.
Notice that I have not included a parameter for number one and I'm just using this notation.
Number two equals three instead of just simply passing a number.
Right.
So let's go for two equals three, and that will give us four.
So addition, does the default one for number one.
And then it used my user parameter for name to which was three.
Of course, you can still override the defaults by.
Providing both addition sanction parameters like this.
OK, so that's about it with an introduction to functions.
Now you are ready to move on to the next lecture where we talk about classes and objects.