Hello and welcome back to an underclass of our course but the complete introduction to Python.
We talked about many things until now.
I think we are gonna learn another thing that will be very very useful into your well in Python programming
so what we are gonna talk about today are functions so basically functions are just a simplified way
to write down your code.
So we will start by learning how we can write functions.
So basically functions will always start with a def so when you write down def this means you are starting
a function.
So you are creating your own function.
Then you write down the name of your function.
So let's say for example your function will say you love something so write down you can name your function
let's say love.
We'll say what exactly you love then open up your disease and inside your parenthesis you will write
down the element that will be used in your function.
So in this case we will say name.
So the permit the parameter will be name.
Then when it's all done you write down two points.
After that you write down what you want to put in your function so let's say you want to print the name.
I love
and then you add name.
So in this case your function is very basic it's something that is very basic.
You have the function which is love you have name which will be the parameter and the parameter used
and your functions output will be print.
I love plus your name.
So in this case name.
So if we run our functions so how do we run functions.
You can't just click here.
It's not gonna work to run your function.
You need to write down your functions name so love.
And in this case your barometer is name.
So you simply have to change it.
In this case a name let's say will be pizza.
So since it's a string you write it down like this and then you can run it.
So you run your function as you can see it's written down.
I love pizza.
So what it does it simply will help you only write down the code once and then you can run your function
as many times as you want.
So right here it's something very very simple.
Once again it's only two lines.
Well it's one line of code.
It's the function plus one line of code so what's happening in the function is a write down print.
I love you.
All right.
So I want that discourse is really to show you how you can use functions and do something very very
cool with them.
So what I propose you today is to write down a monologue between two persons.
So you will have one person that is ordering food.
You will have another person who will tell what type of food this person wants and you will have the
person no one who will answer to this person.
Thank you.
So we'll use only functions to create all this so will create three functions.
In this case we'll have Person 1 and Person 2.
So let's start the first thing we want to create.
Right now we'll be the first function.
So the function for the person number one.
So the first thing we are writing down will be death.
So this is how we start the creation of a function.
So right down there then let's say Person 1.
So in this case per one this will be our person.
So in this case we are one and we write down the name because the only parameter that we will need will
be the name we write down two points.
And what exactly the person one will say the person one will only say well the person one will only
say Hello how can I help you.
So it looks something like a small print will have the name of the person.
So in this case name plus two points because once again it's a monologue and you will write down Hello
how can I help you.
All right.
So we have all this.
So let me just run this small line of code and show you what it looks like.
So we have our first one and this case let's say the name will be cashmere because the person is the
cashier to simply write this down
all right.
When it's already we run the up.
So as you can see the cashier to point said Hello how can I help you.
So that's that's them on the look.
So we have our person no one who is saying Hello how can I help you.
All right.
So you can delete this right now what we want to do.
We will create the person number two.
So the person number two will ask will say what this what she wants or he wants to eat.
So the way I want to make it on I make it a bit more complicated.
So you guys can practice and you will be able to.
Well in this case understand a bit more what you can do with the function.
So I decided to make it more complicated.
We are going to use inputs Swansea and it's not that complicated but you'll see it's going to be more
cool.
So in this case we are going to use input since we are going to ask our user to tell us what he wants
to eat so let's write it down.
So first then we are starting our function which will be the death then we write down the person in
this case to and the first line because the person number two takes only once.
All right.
So what exactly this person will say.
So this person will say the food that he or she wants the drink that dessert and her or his name.
All right.
So those are already parameters that will be used in this function.
So we have four parameters and that's what we will use.
All right.
Next thing we start with the our name since we want the our user to tell us his name.
So the user will tell us what you want.
He wants to eat.
In this case we simply write down name equal and we write down input.
So once again the input function as we saw it before it is a way to ask our user to give us a certain
answer.
So we asked him a question and the user give us an answer.
In this case we write down input and we ask our user what is your name.
So very simple the user answers.
And next thing we want to ask the user what food you want to eat.
So in this case we'll call this variable food because you can see I'm storing variables in my functions
and my functions are.
So I have food in this case will ask our user what type of food you want to eat to input.
And we ask our user what do you want.
What do you want to eat.
All right.
So the user will answer us.
Next thing we'll ask the user what type of drinks he wants.
So we'll name this variable drink.
So as you can see drink when dealing with drink with the drink drink right here would be will be linked
with drink right here and we'll ask our user to write down input and we'll ask our user what exactly
he wants to drink
all right.
What do you want to drink.
Great.
Finally the.
Well the next thing we'll ask our user what type of dessert you want.
So dessert.
And once again we'll do the same thing.
Input
what dessert
do you want.
So right now the person will answer those questions.
What is your name.
What you want to eat what you want to drink.
And finally what desert do you want.
So those are the questions we are asking our user.
The last thing that we want to do in our function is print something.
So what it will be the output of this function.
So when we have all those answers to all this questions.
So right now we'll write down the outputs in this case we'll write down the monologue of this person.
So once again we will start with name.
So since we need the name of the person we'll write down plus and open up the small commas up there
two points since we want to one the name of the person.
Then two points and the person will say I would like the food that the person wants.
So plus in this case food then we continue our sentence.
I want to drink.
So in this case I want to drink
what the person what I drink.
It's the person wants to drink the variable drink next plus
I want.
So in this case the last variable that we will use will be desert.
I want desert the variable desert as a desert.
So we simply open up the parenthesis right here right down as a desert desert.
Here we go.
So what we have now we have our output of our function right here.
What's also with that when you have inputs right there you don't need to write down the well the elements
that are here.
So for example in this case we would need to write down debt.
So let me just show you what it will look like.
So when we print this function so when we print the the function right here we'll need to name our element.
So in this case sorry I don't but one person one and we'll need to print this element right here.
So we'll need to give a name to the name.
So in this case the name can be Jack but in the case of the function right here since we are asking
our user to give us those parameters we don't need to name them.
And even if we name them well what's gonna happen is that python will use the inputs instead of using
our parameters that we wrote where our elements.
All right.
So until now we have our two first monologues.
The last one will be very easy.
So once again it's going to be again the person number two.
So in this case the person number ones are.
In this case we'll write down deaf person number one and the second monologue.
So person number 1 2.
So in this case we just need the name of the person to write down name and the person will say print.
So the name of the person because we want to know who is talking plus we open up our small comments
to points.
Thank you
that's it.
All right.
So right now we have all our functions.
We can cause them so we have created our functions.
The first thing that we want to do is very simple
right now want to write down our code so in this case what we want to do.
As I said we want to write it down.
So how exactly we write it down.
I will simply write down our first person.
So in this case purse two points 1.
So this will be our first person who will talk.
So in this case first one let's say the name of the first one is they said the cashier then
here we go.
Then the second person who will talk will be the person number two.
So in this case person to 1.
And in this case we don't need to name all those variables we can say instead of we can just write the
exact same thing.
So we can just copy and paste this or just write down food then we'll write down the drink
we'll write down dessert in this case dessert
and name.
So those are the four variables that were the four.
Well the four elements that are in our function
and finally we have the person number one and two.
So in this case it's once again the cashier.
Here we have to name our variable.
So yeah sheer all right.
So right now we have everything that we need.
And the only thing that is left to do will be to run our function so we have our cashier who says hello
how can I help you.
So you answer what is your name you say in this case my name is let's say John.
What do you want to eat.
I want to eat pizza.
I want to drink soda.
And what dessert I want I want cake.
So as you can see here we have John.
I would like pizza.
I want to drink soda.
I want a cake as a dessert.
And the cashier says thank you.
As you can see we have our modern monologue that is created.
And as you can see it's well it's very very simple.
We created our functions right here.
All right.
Right now if we change the order so for example if we put the person number to one right here.
Well the person number two starts the monologue.
We will have the person number two that will say we'll have this function at first.
So we'll have our function right here.
If for example we instead take it like this and write it and put it there.
So this would not make sense because in our monologue this is the second thing that the person needs
to say.
So we have.
This is the first thing this is the second thing this is the third thing.
So in our monologue it will not work.
But let's say for example you want to work with let's say you have your functions and you need to start
with this function.
Then this function then this function or this at first and this and this.
Well in this case you can do it.
You can simply change the word.
So the way you will change your order let's say want to start with this one right here.
All right done.
So person number one so the function number three as you can see we start with this once once again
fear we'll say cashier and then we can go to the person number one and then well to the function number
one and the come back the function number two so you decide of the order that you want to use your functions.
So as I explained functions are simply a way for you guys to store not just for information but to store
let's say a certain calculation or whatever you want to store in it what's cool also with functions
that you can store anything you want.
You can have variables in them.
You can have strings.
You can have boolean.
You can have numbers and that you can use them for absolutely everything.
So let's say you want to write down a line of code that you know that you will reuse instead of writing
down a huge code right here and copy it and passed it everywhere.
You can just write down the function and reuse simply this function.
So right now you understand what is the concept of functions and how exactly they can help you.
We'll see later in the course some more cool stuff that we can do with functions.
But until now you know that they exist and they're are just a good way to store information and calculations
in them.
That's it for this class guys.
And see open our next class.